在Mongoose子数组中查询对象

我有一个架构,看起来像这样:

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); 

所有的ID都是唯一的。 区域内可以有多个库(另一个数组)。

我的问题是,我将如何查询嵌套数组来获得所需的对象? 更确切地说,我将如何得到一个特定的subCollection对象,给定一个库ID,集合ID和subCollection ID。

你可以使用这个查询:

 Library.find({ 'id': libraryID, 'collections.id': CollectionID, 'collections.subCollections.id': subCollectionID }, { 'collections.subCollections.$': 1 }, function(err, data) { console.log(err, data); })