MongoDB/Mongoose 一对多,参考孩子中的父母 - 分组并加入

use*_*169 3 mongoose mongodb mongodb-query mongoose-populate

我按照 Mongo 文档中的建议在子集合中参考父集合,因为子集合有增长的前景。 https://docs.mongodb.com/manual/tutorial/model-referenced-one-to-many-relationships-between-documents/

发布者(父)集合:

{
   _id: "oreilly",
   name: "O'Reilly Media",
   founded: 1980,
   location: "CA"
}
Run Code Online (Sandbox Code Playgroud)

图书(儿童)收藏 - 参考其中的出版商:

{
   _id: 123456789,
   title: "MongoDB: The Definitive Guide",
   author: [ "Kristina Chodorow", "Mike Dirolf" ],
   published_date: ISODate("2010-09-24"),
   pages: 216,
   language: "English",
   publisher_id: "oreilly"
},{
   _id: 234567890,
   title: "50 Tips and Tricks for MongoDB Developer",
   author: "Kristina Chodorow",
   published_date: ISODate("2011-05-06"),
   pages: 68,
   language: "English",
   publisher_id: "oreilly"
}
Run Code Online (Sandbox Code Playgroud)

现在我想要实现的是让所有出版商拥有他们的书籍子数组:

    {
  "_id": "oreilly",
  "name": "O'Reilly Media",
  "founded": 1980,
  "location": "CA",
  "books": [
    {
      "_id": 123456789,
      "title": "MongoDB: The Definitive Guide",
      "author": [
        "Kristina Chodorow",
        "Mike Dirolf"
      ],
      "published_date": "2010-09-24",
      "pages": 216,
      "language": "English",
      "publisher_id": "oreilly"
    },
    {
      "_id": 234567890,
      "title": "50 Tips and Tricks for MongoDB Developer",
      "author": "Kristina Chodorow",
      "published_date": "2011-05-06",
      "pages": 68,
      "language": "English",
      "publisher_id": "oreilly"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我使用猫鼬,并且我知道如果我在出版商中存储了一系列书籍引用,我可以完成 .populate("books") - 我不想这样做,因为书籍会不断增加。我想知道如何在书籍收藏中出现出版商参考的情况下实现相同的结果。

Jul*_*SIN 5

您必须使用$lookup来执行连接。

db.publisher.aggregate([{$lookup: {from: 'books', localField: '_id', foreignField: 'publisher_id', as: 'books'}} ]).pretty()
{
        "_id" : "oreilly",
        "name" : "O'Reilly Media",
        "founded" : 1980,
        "location" : "CA",
        "books" : [
                {
                        "_id" : 123456789,
                        "title" : "MongoDB: The Definitive Guide",
                        "author" : [
                                "Kristina Chodorow",
                                "Mike Dirolf"
                        ],
                        "published_date" : ISODate("2010-09-24T00:00:00Z"),
                        "pages" : 216,
                        "language" : "English",
                        "publisher_id" : "oreilly"
                },
                {
                        "_id" : 234567890,
                        "title" : "50 Tips and Tricks for MongoDB Developer",
                        "author" : "Kristina Chodorow",
                        "published_date" : ISODate("2011-05-06T00:00:00Z"),
                        "pages" : 68,
                        "language" : "English",
                        "publisher_id" : "oreilly"
                }
        ]
}
Run Code Online (Sandbox Code Playgroud)