繁体   English   中英

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

[英]Get a deep-nested array from a mongodb aggregate and include in result

假设我有这两个 collections

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
},

我已经尝试使用此代码进行聚合,但它只是将每本书的每个书签分开并将其推送到 object 中。

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"
      }
    }
  }
])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM