【问题标题】:Querying for Object in Mongoose Sub Array查询 Mongoose 子数组中的对象
【发布时间】:2016-06-07 05:33:53
【问题描述】:

我有一个如下所示的架构:

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 都是唯一的。一个区域(另一个数组)内可以有多个库。

我的问题是,我将如何查询嵌套数组以获取所需的对象?更准确地说,在给定库 ID、集合 ID 和子集合 ID 的情况下,我将如何获取特定的子集合对象。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    你可以使用这个查询:

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

    【讨论】:

    • 一个解释会很有用
    猜你喜欢
    • 2014-05-13
    • 2021-07-02
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多