我正在尝试将 a 转换String
为ObjectId
:
var _id=mongoose.Types.ObjectId(req.body.notebook);
notebook 的值实际上是文档中的 id database
:
> db.notebook.findOne()
{
"title" : "My Notebook",
"isActive" : false,
"_id" : ObjectId("54505ced1fa5b1b519bdfc88"),
"notes" : [ ],
"__v" : 0
}
我正在调试,方法如下所示:
mongoose.types.ObjectId("54505ced1fa5b1b519bdfc");
我在调试时遇到了这样的错误:
ObjectId must either be a 12 byte string or 24 hex charecters.
该错误发生在方法中ObjectId.isValid
。我尝试使用以下方法进行转换:
mongoose.Types.ObjectId.fromString(req.body.notebook);
和
mongoose.mongo.BSONPure.ObjectID.fromString(req.body.notebook);
我知道我必须将此字符串转换为适当的十六进制格式。我该怎么做?