0

I have following Collection

Location Collection

 [
 {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}

]

Is any query we throw and get immediate node of location using above data.

Example. When I said India it should be return

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

Thank you

Avinash
  • 137
  • 1
  • 10

1 Answers1

0

Using Aggregation framework we can get this desired result.

The below query gives us the result and in this query I have used $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"}}
])

Documents in my collection

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

After executing the above query, the result is

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

If we cut down the last $match in our query, then it will return the complete the grouping of states.

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

The result of the above query is

{
        "_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"
                }
        ]
}
Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34
  • ...Thank you very much – Avinash Aug 28 '18 at 10:49
  • ...In first query what if location pune has also child Baner,Deccan,...etc – Avinash Aug 28 '18 at 11:10
  • In that case this query will bring us, pune as another document with its child in array, like `{ "_id" : ObjectId("5b83e4860c35ef57411a575b"), "location" : "pune", "parentLocation" : "Maharashstra", "Result" : [ { "location" : "Baner" }, { "location" : "Deccan" } ] }` . – Clement Amarnath Aug 28 '18 at 11:35
  • No, it will not create child of pune....as you mentioned...it was only showing one level...i think – Avinash Aug 29 '18 at 03:27