当mongoose模型是新的时,有没有办法自动生成ObjectId?

时间:2021-01-12 02:33:55

is there a way to declare a Model schema in mongoose so that when the model is new'ed the _id field would auto-generate?

有没有办法在mongoose中声明一个Model模式,这样当模型新建时,_id字段会自动生成?

for example:

例如:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectIdSchema = Schema.ObjectId;
var ObjectId = mongoose.Types.ObjectId;

var PersonSchema = new Schema({
    _id:            ObjectIdSchema,
    firstName:      {type: String, default: 'N/A'},
    lastName:       {type: String, default: 'N/A'},
    age:            {type: Number, min: 1}
});

var Person = mongoose.model('Person', PersonSchema);

at first, i thought great!, i'll just do

起初,我觉得很棒!,我会做的

_id:    {type:ObjectIdSchema, default: new ObjectId()}

but of course that doesn't work, because new ObjectId() is only called on initialize of schema. so calling new Persion() twice creates two objects with the same _id value.

但当然这不起作用,因为新的ObjectId()仅在初始化模式时调用。所以调用新的Persion()两次会创建两个具有相同_id值的对象。

so is there a way to do it so that every time i do "new Person()" that a new ObjectId() is generated?

那么有没有办法做到这一点,每次我做“new Person()”生成一个新的ObjectId()?

the reason why i'm trying to do this is because i need to know the value of the new person's _id value for further processing.

我试图这样做的原因是因为我需要知道新人的_id值的值,以便进一步处理。

i also tried:

我也尝试过:

var person = new Person({firstName: "Joe", lastName: "Baz"});
person.save(function(err, doc, num){
    console.log(doc._id);
});

even then, doc doesn't contain the ObjectId. but if i look in the database, it does contain it.

即使这样,doc也不包含ObjectId。但如果我查看数据库,它确实包含它。

p.s. i'm using mongoose 2.7.1

附:我正在使用猫鼬2.7.1

p.p.s. i know i can manually create the ObjectId when creating the person as such:

p.p.s.我知道我可以在创建人员时手动创建ObjectId:

var person = new Person({_id: new ObjectId(), firstName: "Joe", lastName: "Baz"});

but i rather not have to import ObjectId and have to new it every time i want to new a Person. guess i'm used to using the java driver for mongodb, where i can just create the value for the _id field in the Model constructor.

但我宁愿不必导入ObjectId,每次我想要新建一个Person时都必须新建它。我以前习惯使用mongodb的java驱动程序,我可以在模型构造函数中为_id字段创建值。

3 个解决方案

#1


18  

the moment you call var person = new Person(); person._id should give you the id (even if it hasn't been saved yet). Just instantiating it is enough to give it an id. You can still save it after, and that will store the id as well as the rest of the person object

你调用var person = new Person(); person._id应该给你id(即使它尚未保存)。只是实例化它就足以给它一个id。你仍然可以在之后保存它,这将存储id以及person对象的其余部分

#2


9  

Add the auto flag:

添加自动标志:

_id: {
    type: mongoose.Schema.Types.ObjectId,
    index: true,
    required: true,
    auto: true,
  }

source

资源

#3


7  

Instead of:

代替:

_id:    {type:ObjectIdSchema, default: new ObjectId()}

You should do:

你应该做:

_id:    {type:ObjectIdSchema, default: function () { return new ObjectId()} }

#1


18  

the moment you call var person = new Person(); person._id should give you the id (even if it hasn't been saved yet). Just instantiating it is enough to give it an id. You can still save it after, and that will store the id as well as the rest of the person object

你调用var person = new Person(); person._id应该给你id(即使它尚未保存)。只是实例化它就足以给它一个id。你仍然可以在之后保存它,这将存储id以及person对象的其余部分

#2


9  

Add the auto flag:

添加自动标志:

_id: {
    type: mongoose.Schema.Types.ObjectId,
    index: true,
    required: true,
    auto: true,
  }

source

资源

#3


7  

Instead of:

代替:

_id:    {type:ObjectIdSchema, default: new ObjectId()}

You should do:

你应该做:

_id:    {type:ObjectIdSchema, default: function () { return new ObjectId()} }