MongoDB: 我可以在保存文档之前使用创建的 ObjectId
MongoDB: Can I use a created ObjectId before saving my document
在 node.js 上使用 mongoose
:
我可以在模型保存到数据库之前访问模型的_id
字段并确保ID不会改变吗?
例如,我想执行以下操作:
var model = new mongoose.model('MyModel');
someOtherObject.myModelId = String(model._id);
// Some more code...
model.save(...);
在 Mongoose 中,_id
值必须始终分配给客户端,因为 the docs 表示:
Mongoose forces the db option forceServerObjectId
false and cannot be overridden.
因此,除非您在调用 model.save
.
之前在自己的代码中执行此操作,否则您在代码第一行中获得的 model._id
值将不会更改
在 node.js 上使用 mongoose
:
我可以在模型保存到数据库之前访问模型的_id
字段并确保ID不会改变吗?
例如,我想执行以下操作:
var model = new mongoose.model('MyModel');
someOtherObject.myModelId = String(model._id);
// Some more code...
model.save(...);
在 Mongoose 中,_id
值必须始终分配给客户端,因为 the docs 表示:
Mongoose forces the db option
forceServerObjectId
false and cannot be overridden.
因此,除非您在调用 model.save
.
model._id
值将不会更改