Reputation: 21
I have a Schema that looks like this:
var LibrarySchema = new Schema({
id: String,
contactNumber: String,
collections: [{
id: String,
description: String
subCollections: [{
id: String,
description: String,
recentlyUpdated: Boolean
}, {
id: String,
description: String,
recentlyUpdated: Boolean
}]
}]
})
module.exports = mongoose.model('Library', LibrarySchema);
All the ID's are unique. There can be multiple Libraries inside a District (another array).
My question is, how would I query into the nested arrays to get the desired object? To be more precise, how would I get a specific subCollection object, given a Library ID, Collection ID, and subCollection ID.
Upvotes: 2
Views: 3268
Reputation: 4700
You can use this query:
Library.find({
'id': libraryID,
'collections.id': CollectionID,
'collections.subCollections.id': subCollectionID
}, { 'collections.subCollections.$': 1 }, function(err, data) {
console.log(err, data);
})
Upvotes: 3