0

我正在运行以下代码:

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 } }  

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

4

1 回答 1

0

尝试这样的查询:

req.query = {
    "toId.id": oid,
    deleted : false
};  
于 2015-10-12T08:20:28.037 回答