mongodb - 从 mongodb 聚合获取深度嵌套数组并包含在结果中

标签 mongodb mongoose nosql aggregation-framework

假设我有这两个集合

book: {
    _id: 'aaa'
    name: 'Book 1',
    chapters: [
       0: {
           _id: 'chapter0',
           name: 'Chapter 1',
           pages: [
                0: {
                    _id: 'page0',
                    name: 'Page 1',
                    paragraphs: [
                        0: {
                            _id: 'paragraph0',
                            name: 'Paragraph 1',
                            bookmarks: [
                                 0: {sentence: 3, reader: 'Foo'},
                                 1: {sentence: 8, reader: 'Bar'},
                                 2: {sentence: 14, reader: 'John'}
                            ]
                        }
                    ]
                }
           ]
       }
    ]
}



book: {
    _id: 'bbb'
    name: 'Book 2',
    chapters: [
       0: {
           _id: 'chapter0',
           name: 'Chapter 1',
           pages: [
                0: {
                    _id: 'page0',
                    name: 'Page 1',
                    paragraphs: [
                        0: {
                            _id: 'paragraph0',
                            name: 'Paragraph 1',
                            bookmarks: []
                        },
                        1: {
                            _id: 'paragraph1',
                            name: 'Paragraph 2',
                            bookmarks: [
                                 0: {sentence: 2, reader: 'George'},
                                 1: {sentence: 1, reader: 'Paul'},
                                 2: {sentence: 76, reader: 'John'},
                                 3: {sentence: 54, reader: 'Ringo'}                                 
                            ]
                        }
                    ]
                }
           ]
       }
    ]
}

我希望能够在获取结果时提取数组 bookmarks 并将它们附加到 book 集合中。像这样的东西会很好:

{
    id: 'aaa'
    name: 'Book 1'
    bookmarks: [{...}, {...}, {...}] //since the first book has 3 bookmarks
},
{
    id: 'bbb'
    name: 'Book 2'
    bookmarks: [{...}, {...}, {...}, {...}] //since the second book has 4 bookmarks
},

如果没有书签,它应该看起来像:

{
    id: 'aaa'
    name: 'Book 1'
    bookmarks: [{...}, {...}, {...}] //since the first book has 3 bookmarks
},
{
    id: 'bbb'
    name: 'Book 2'
    bookmarks: [{...}, {...}, {...}, {...}] //since the second book has 4 bookmarks
},
{
    id: 'ccc'
    name: 'Book 3'
    bookmarks: [] //third book does not have bookmarks for example
},

我尝试过使用此代码进行聚合,但它只是将每本书的每个书签分开并将其插入对象中。

return yield Books.aggregate()
    .unwind('chapters')
    .unwind('chapters.pages')
    .unwind('chapters.pages.paragraphs')
    .unwind('chapters.pages.paragraphs.bookmarks')
    .group({
        _id: '$_id',
        books: {
            $push: {
                _id: '$_id',
                name: '$name',
                bookmarks: '$chapters.pages.paragraphs.bookmarks'
            }
        }
    }).exec()

有人能指出我正确的方向吗?谢谢!

最佳答案

尝试下面的聚合管道:

Books.aggregate([
  {
    $unwind: "$book"
  },
  {
    $unwind: "$book.chapters"
  },
  {
    $unwind: "$book.chapters.pages"
  },
  {
    $unwind: "$book.chapters.pages.paragraphs"
  },
  {
    $unwind: {
      path: "$book.chapters.pages.paragraphs.bookmarks",
      preserveNullAndEmptyArrays: true
    }
  },
  {
    $group: {
      _id: {
        _id: "$_id",
        book: "$book.name"
      },
      bookmarks: {
        $push: "$book.chapters.pages.paragraphs.bookmarks"
      }
    }
  }
])

关于mongodb - 从 mongodb 聚合获取深度嵌套数组并包含在结果中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58076291/

相关文章:

node.js - Mongoose :查找与条件不匹配的文档和修剪数组元素

javascript - 聚合对象数组中的数据

javascript - Meteor Blaze - 区分类别

mongodb - Mongo DB 结果接口(interface)到 Golang 中的结构转换

javascript - 使用 $http.get 获取嵌套 JSON 中的数组(mongoose 和 nodejs)

xpath - 从大型数据库返回唯一元素值

Oracle (RAC) 与 NoSQL

mongodb - 我应该将 NoSQL 数据库拆分到不同的服务器上吗?

javascript - meteor upsert 返回错误

javascript - Mongoose 保存是异步的吗?