【问题标题】:How to find Immediate Child of a node in mongo db如何在 mongodb 中找到节点的直接子节点
【发布时间】:2018-08-26 02:26:55
【问题描述】:

我有以下收藏

位置集合

 [
 {id : 1 name : 'l1' , 'location' : pune , parentLocation : Maharashstra},
 {id : 2 name : 'l2' , 'location' : nashik , parentLocation : Maharashstra},
 {id : 3 name : 'l3' , 'location' : mumbai , parentLocation : Maharashstra},
 {id : 4 name : 'l4' , 'location' : Maharashstra , parentLocation : India},
 {id : 5 name : 'l5' , 'location' : India , parentLocation : null}

]

我们是否使用上述数据抛出任何查询并获取位置的直接节点。

示例。 当我说印度时,它应该返回

India
 |---Maharashtra
      |---Pune
           |---..
      |---Nashik
      |---Mumbai

谢谢

【问题讨论】:

  • 您想获取India 的所有子位置还是只获取Maharashstra 的直接位置?
  • @MạnhQuyếtNguyễn ,取决于搜索键...如果 key = india 那么所有印度的孩子.....如果 key=Maharashtra那么所有maharashrta的孩子......

标签: mongodb mongodb-query aggregation-framework aggregate nosql-aggregation


【解决方案1】:

使用Aggregation framework,我们可以得到想要的结果。

下面的查询给出了结果,在这个查询中我使用了$lookup$match$project

db.location.aggregate([
   { 
     $lookup: {
       from: "location", 
       localField: "location", 
       foreignField: "parentLocation", 
       as:"Result"
     } 
   },
   {$match:{ "Result": {$exists:true, $ne:[]}, parentLocation: {$ne: null} }}, 
   {$project :{ parentLocation:1, location:1, "Result.location":1}},
   {$match: {location: "Maharashstra"}}
])

我收藏的文档

{ "_id" : ObjectId("5b83e4860c35ef57411a575b"), "id" : 1, "name" : "l1", "location" : "pune", "parentLocation" : "Maharashstra" }
{ "_id" : ObjectId("5b83e4860c35ef57411a575c"), "id" : 2, "name" : "l2", "location" : "nashik", "parentLocation" : "Maharashstra" }
{ "_id" : ObjectId("5b83e4860c35ef57411a575d"), "id" : 3, "name" : "l3", "location" : "mumbai", "parentLocation" : "Maharashstra" }
{ "_id" : ObjectId("5b83e4860c35ef57411a575e"), "id" : 4, "name" : "l4", "location" : "Maharashstra", "parentLocation" : "India" }
{ "_id" : ObjectId("5b83e4860c35ef57411a575f"), "id" : 5, "name" : "l5", "location" : "India", "parentLocation" : null }
{ "_id" : ObjectId("5b83fec90c35ef57411a5760"), "id" : 6, "name" : "l6", "location" : "Chennai", "parentLocation" : "Tamilnadu" }
{ "_id" : ObjectId("5b83fec90c35ef57411a5761"), "id" : 7, "name" : "l7", "location" : "Trichy", "parentLocation" : "Tamilnadu" }
{ "_id" : ObjectId("5b83fec90c35ef57411a5762"), "id" : 8, "name" : "l8", "location" : "Alapuzha", "parentLocation" : "Kerala" }
{ "_id" : ObjectId("5b83fec90c35ef57411a5763"), "id" : 9, "name" : "l9", "location" : "Kerala", "parentLocation" : "India" }
{ "_id" : ObjectId("5b83fec90c35ef57411a5764"), "id" : 10, "name" : "l10", "location" : "Tamilnadu", "parentLocation" : "India" }

执行上述查询后,结果为

{
        "_id" : ObjectId("5b83e4860c35ef57411a575e"),
        "location" : "Maharashstra",
        "parentLocation" : "India",
        "Result" : [
                {
                        "location" : "pune"
                },
                {
                        "location" : "nashik"
                },
                {
                        "location" : "mumbai"
                }
        ]
}

如果我们减少查询中的最后一个 $match,那么它将返回完整的状态分组。

db.location.aggregate([
       { 
         $lookup: {
           from: "location", 
           localField: "location", 
           foreignField: "parentLocation", 
           as:"Result"
         } 
       },
       {$match:{ "Result": {$exists:true, $ne:[]}, parentLocation: {$ne: null} }}, 
       {$project :{ parentLocation:1, location:1, "Result.location":1}}
    ])

上面查询的结果是

{
        "_id" : ObjectId("5b83e4860c35ef57411a575e"),
        "location" : "Maharashstra",
        "parentLocation" : "India",
        "Result" : [
                {
                        "location" : "pune"
                },
                {
                        "location" : "nashik"
                },
                {
                        "location" : "mumbai"
                }
        ]
}
{
        "_id" : ObjectId("5b83fec90c35ef57411a5763"),
        "location" : "Kerala",
        "parentLocation" : "India",
        "Result" : [
                {
                        "location" : "Alapuzha"
                }
        ]
}
{
        "_id" : ObjectId("5b83fec90c35ef57411a5764"),
        "location" : "Tamilnadu",
        "parentLocation" : "India",
        "Result" : [
                {
                        "location" : "Chennai"
                },
                {
                        "location" : "Trichy"
                }
        ]
}

【讨论】:

  • ...非常感谢
  • ...首先查询如果位置 pune 也有子 Baner,Deccan,...等怎么办
  • 在这种情况下,这个查询将把 pune 作为另一个文档,它的子元素在数组中,比如 { "_id" : ObjectId("5b83e4860c35ef57411a575b"), "location" : "pune", "parentLocation" : "Maharashstra", "Result" : [ { "location" : "Baner" }, { "location" : "Deccan" } ] }
  • 不,它不会创建 pune 的子级......正如你提到的......它只显示一个级别......我认为
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 2014-07-21
相关资源
最近更新 更多