【发布时间】:2019-06-23 13:47:27
【问题描述】:
有时 mongoose 将“main_id”保存为字符串而不是 ObjectId,保存后我在 mongoshell 和 UI 客户端中看到字符串类型。
猫鼬:4.11.0
我有架构:
var building = new Schema({
location: {
main_id: { type: Schema.Types.ObjectId, ref: 'locations'},
title: {type: String},
description: {type: String}
},
year: {type: Number}
});
var User = new Schema({
my_location: {
now: building,
more: [building]
}
})
当下面的代码运行时,有时我在 DB 中得到字符串而不是 objectId。
user.my_location.more[i].location.main_id = new mongoose.Types.ObjectId("584fe811ed936fe74d8b470e")
user.save()
当我在保存后检查 typeof user.my_location.more[i].location.main_id 时说“对象”但在 DB 中显示字符串:) 并且当我在 mongoDB 中使用 ObjectId 运行查询时,我找不到这个更新的文档
【问题讨论】: