我正在运行以下代码:
var oid = new mongoose.Types.ObjectId(req.params.id);
req.query = {
toId : {
id: oid
},
deleted : false
};
X.find(req.query, function (err, docs){
if(!err){
res.status(responses.ok.status).json(docs);
}
else{
return next(errorHelper(err));
}
}).select(visibleFields.X);
我想通过toId.id
字段查询X模型。 toId
字段的类型为Y.以下是模型定义:
var X = new Schema({
deleted: {
type: Boolean,
required: true,
default: false
},
toId: {
type: entityId,
required: true
}
});
Y只是一个定义为:
的对象var Y = {
id: {
type: Schema.Types.ObjectId,
required: true
},
entityName:{
type: String,
required: true,
enum: entityTypeList
}
};
在我的数据库中,我有一个条目:
{
"_id" : ObjectId("55d1f4fddaea17d3e90a3c55"),
"toId" : {
"id" : ObjectId("55dde27a0f6ff71b72dc7981"),
"entity" : "user"
},
"deleted" : false
}
无论如何,应该满足我的标准。
但是,以下查询:
messages.find({ deleted: false, toId: { id: ObjectId("55dde27a0f6ff71b72dc7981") } }) { fields: { messageType: 1, toId: 1, timesOff: 1, role: 1, read: 1, productId: 1, participantId: 1, oldRole: 1, message: 1, memberId: 1, lessonId: 1, important: 1, fromId: 1, formerInstructorId: 1, timestamp: 1, changedFields: 1, bookerId: 1, assignedInstructorId: 1, accepted: 1, _id: 1 } }
返回一个空列表。为什么会这样?
答案 0 :(得分:0)
尝试这样的查询:
req.query = {
"toId.id": oid,
deleted : false
};