为什么在 Mongoose 中查询子文档时没有返回文档?
Why is no document returned while querying a sub-doc in Mongoose?
我是运行以下代码:
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 } }
returns 一个空列表。为什么会这样?
尝试这样的查询:
req.query = {
"toId.id": oid,
deleted : false
};
我是运行以下代码:
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 } }
returns 一个空列表。为什么会这样?
尝试这样的查询:
req.query = {
"toId.id": oid,
deleted : false
};