You can find the ObjectId
constructor on require('mongoose').Types
. Here is an example:
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId();
id
is a newly generated ObjectId
.
Note: As Joshua Sherman points out, with Mongoose 6 you must prefix the call with new
:
var id = new mongoose.Types.ObjectId();
You can read more about the Types
object at Mongoose#Types documentation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…