MongoDB/Mongoose 一对多,在子节点中引用父节点 - 分组并加入

Posted

技术标签:

【中文标题】MongoDB/Mongoose 一对多,在子节点中引用父节点 - 分组并加入【英文标题】:MongoDB/Mongoose One-to-Many with refence to parent in child - group and join 【发布时间】:2017-09-04 14:22:07 【问题描述】:

我按照 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"

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


   _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"

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

    
  "_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"
    
  ]

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

【问题讨论】:

【参考方案1】:

您必须使用$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"
                
        ]

【讨论】:

以上是关于MongoDB/Mongoose 一对多,在子节点中引用父节点 - 分组并加入的主要内容,如果未能解决你的问题,请参考以下文章

Mongodb mongoose节点js模式关系

基于用户类型的有条件返回值的结构化节点 API(express、mongodb、mongoose)

为啥当我在 grails 上删除一对多关系上的父级时,会在子级上调用 beforeInsert 事件?

MongoDB/Mongoose:多对多关系

多用户账户的 MongoDB/Mongoose 模式

使用嵌套字段在MongoDB / Mongoose中创建多对多关系?