为什么在查询Mongoose中的子文档时没有返回文档?

时间:2021-09-24 02:35:04

I am running the following code:

我正在运行以下代码:

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);

I would like to query the X model by its toId.id field. toId field is of type Y. Here are the model definitions:

我想通过其toId.id字段查询X模型。 toId字段的类型为Y.以下是模型定义:

var X = new Schema({
deleted: {
    type: Boolean, 
    required: true, 
    default: false
},      
toId: {
    type: entityId,
    required: true
}   
});

Y is just an object defined as:

Y只是一个定义为的对象:

var Y = {
id: {
    type: Schema.Types.ObjectId,
    required: true
},
entityName:{
    type: String,
    required: true,
    enum: entityTypeList
}
};

In my database, I have an entry like:

在我的数据库中,我有一个条目,如:

{
"_id" : ObjectId("55d1f4fddaea17d3e90a3c55"),

"toId" : {
    "id" : ObjectId("55dde27a0f6ff71b72dc7981"),
    "entity" : "user"
},

"deleted" : false

}

which should, by all means, satisfy my criteria.

无论如何,这应该符合我的标准。

However, the following query:

但是,以下查询:

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 an empty list. Why is that happening?

返回一个空列表。为什么会这样?

1 个解决方案

#1


0  

Try a query like this:

尝试这样的查询:

req.query = {
    "toId.id": oid,
    deleted : false
};  

#1


0  

Try a query like this:

尝试这样的查询:

req.query = {
    "toId.id": oid,
    deleted : false
};