mongoose - query on a field that exists inside of another field

Mouaad Ait Said Source

I have this schema in 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'}
});

What I wanna do is query on the steps>tasks>task>_id field to get answers matching certain id x, how can I do this?

node.jsmongodbmongoose

Answers

answered 8 months ago Daphoque #1

Mongo will automatically search into your array:

{"steps.tasks.task._id": Your id}

comments powered by Disqus