mongoose - 查询存在于另一个字段内的字段

时间:2018-02-14 13:53:06

标签: node.js mongodb mongoose

我在mongoose中有这个架构:

 var AnswerSchema = new Schema({
    name : {type : String,required: true},
    study : {type: Schema.Types.ObjectId,ref: 'Study'},
    model : {type: Schema.Types.ObjectId,ref: 'Model'},
    entity : {type : String, required : true},
    opened : {type: Boolean, default : true},
    steps : [{
        step : {_id: {type : String}, name: {type : String}, color: {type : String}, fields: {}},
        tasks : [{
            task:{_id: {type : String}, name: {type : String}, parent: {type : String}, prefix : {type : String , unique : true}},
            progress : {type: Number , min: 0, max: 100, default : 0},
            attachments:[],
            comments:[],
            fields:{},
            predecessors:[],
        }],
        cloned: {type: Boolean, default : false}
    }],
    lastReview : {
        date:{type: String},
        completed : [],
        statusUpdated : [],
        dueDateUpdated : []
    },
    content: {},
    creationDate : {type: Date,default: Date.now},
    lastUpdateDate : Date,
    createdBy : {type: Schema.Types.ObjectId,ref: 'User'},
    lastUpdatedBy : {type: Schema.Types.ObjectId,ref: 'User'}
});

我想做的是查询步骤>任务>任务> _id字段以获得与某个id x匹配的答案,我该怎么做?

1 个答案:

答案 0 :(得分:0)

Mongo会自动搜索你的数组:

{“steps.tasks.task._id”:您的身份}