I am developing an program with 2 models, building and floor. The schema are defined as following.
var BuildingSchema = new Schema({
block:{type:String,trim:true},
project_id:{type:String},
floors:[{type:Schema.Types.ObjectId,ref:'Floor'}]
})
var FloorSchema = new Schema({
name:{type:String,trim:true},
building_id:{type:String,ref:'Building'}
}
What I would like to do is to find the floor result with the conditions of floor.name and building.project_id. I have tried this but didn't work
floor.find({name:'fname','building_id.project_id':123}).exec()
How do I get what I want? Thanks.
You'll need to make use of $elemMatch to retrieve just the floor. The mongoosejs docs talk about it here .
So you might do something like:
building.find({"project_id": 123}).where("floors").elemMatch(function(elem){
elem.match("name", "fname");
});
This should only return the floor element that matches {"name": "fname"}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.