更新时间:2023-02-17 12:28:20
您可以在 require('mongoose').Types
上找到 ObjectId
构造函数.下面是一个例子:
You can find the ObjectId
constructor on require('mongoose').Types
. Here is an example:
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId();
id
是新生成的ObjectId
.
注意:作为 Joshua Sherman 指出,对于 Mongoose 6,您必须在调用前加上 new
:
Note: As Joshua Sherman points out, with Mongoose 6 you must prefix the call with new
:
var id = new mongoose.Types.ObjectId();
您可以在 Mongoose#Types 文档中阅读有关 Types
对象的更多信息.
You can read more about the Types
object at Mongoose#Types documentation.