【问题标题】:Query in MongoDB with MutliCollection Aggregation, Joins在 MongoDB 中使用 MutliCollection 聚合、连接进行查询
【发布时间】:2016-05-25 19:02:13
【问题描述】:

我想在 MongoDb 上进行查询,例如 Select * from Person, School WHERE Person.pID == School.pID。我怎样才能在 MongoDB 上做到这一点。使用 Scrapy,我已经废弃了这个 page 并且已经有了 Collection,我想在 Collection 和 Mongoose 之间建立外键关系。我怎样才能在这个给定的例子上做到这一点?。谢谢。 (第一个集合是学校,第二个集合是人)

/* School Collection */
{
    "_id" : ObjectId("574453fb085516029132205a"),
    "involvedInst" : [ 
        "10403"
    ],
    "instID" : [ 
        "10359"
    ],
    "projID" : [ 
        "271289"
    ],
    "fSpeaker" : [],
    "pID" : [ 
        "1229134"
    ],
    "speaker" : [ 
        "Professor Dr. Georg  Reiser"
    ]
}

/* PersonCollection */
{
    "_id" : ObjectId("574453fb085516029132205a"),
    "beteiligteInstituten" : [ 
        "10403"
    ],
    "instID" : [ 
        "10359"
    ],
    "projID" : [ 
        "271289"
    ],
    "fachlicherDFGAnsprechpartner" : [],
    "pID" : [ 
        "1229134"
    ],
    "speaker" : [ 
        "Professor Dr. Georg  Reiser"
    ]
}

【问题讨论】:

标签: node.js mongodb schema


【解决方案1】:

在 3.2 版中,有一个新功能可以在聚合函数中查找另一个集合。 https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

在您的情况下,您可以像这样使用 $lookup:

db.person.aggregate([
    {$lookup:
       {
          from: "school",
          localField: "pId",
          foreignField: "pId",
          as: "person_school"
       } 
    }
])

文档将返回为

{
    "_id" : ObjectId("5745805a39c36b72fb8e34a9"),
    "beteiligteInstituten" : [
            "10403"
    ],
    "instID" : [
            "10359"
    ],
    "projID" : [
            "271289"
    ],
    "fachlicherDFGAnsprechpartner" : [ ],
    "pID" : [
            "1229134"
    ],
    "speaker" : [
            "Professor Dr. Georg  Reiser"
    ],
    "person_school" : [
            {
                    "_id" : ObjectId("5745802a39c36b72fb8e34a8"),
                    "involvedInst" : [
                            "10403"
                    ],
                    "instID" : [
                            "10359"
                    ],
                    "projID" : [
                            "271289"
                    ],
                    "fSpeaker" : [ ],
                    "pID" : [
                            "1229134"
                    ],
                    "speaker" : [
                            "Professor Dr. Georg  Reiser"
                    ]
            }
    ]
}

可以跨集合使用多个查找。

db.person.aggregate([
    {
        $lookup:
            {
            from: "school",
            localField: "pId",
            foreignField: "pId",
            as: "person_school"
        }
   },
   {
        $lookup:
            {
            from: "teacher",
            localField: "pId",
            foreignField: "pId",
            as: "person_teacher"
        }
   }
])

但您应该考虑使用嵌入式文档或文档引用。 https://docs.mongodb.com/v3.0/tutorial/model-referenced-one-to-many-relationships-between-documents/

【讨论】:

  • @Nodari 谢谢。如何从三个集合中进行连接?
  • 使用可以在聚合管道中使用多个 $lookup。
  • 如何使用嵌入文档,当我已经用 scrapy 报废它或用 dbrefs 引用它时。当在给定的示例中 pID 是 外键和主键 时,dbref 应该如何显示
猜你喜欢
  • 2017-02-16
  • 1970-01-01
  • 2022-11-08
  • 2023-03-28
  • 2019-10-13
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
相关资源
最近更新 更多