मेरे पास डेटा सेट है

db.users.insertMany([ {"_id":1, "name":"abcd"}, {"_id":2, "name":"abcd"}, {"_id":3, "name": "abcd" }, {"_id":4, "name":"abcd"}, {"_id":5, "name":"abcd" }, {"_id":6, "name":"abcd "}, {"_id":7, "name":"abcd" }, {"_id":8, "name":"abcd"}, {"_id":9, "name":"abcd"} , {"_id":10, "name":"abcd" }, {"_id":11, "name":"abcd"}, {"_id":12, "name":"abcd"}, { "_id":13, "name":"abcd"}, {"_id":14, "name":"abcd"}, {"_id":15, "name":"abcd"}, {"_id ":16, "नाम":"abcd"},])

db.users_hirarchy.insertMany([{"_id": "1101", "_from": 14, "_to": 15 },{ "_id": "1102", "_from": 14, "_to": 16 } ,{ "_id": "1103", "_from": 15, "_to": 3 },{ "_id": "1104", "_from": 15, "_to": 5 },{ "_id": "1105", "_from": 15, "_to": 7 },{ "_id": "1106", "_from": 3, "_to": 1},{ "_id": "1107", "_from ": 3, "_to": 2},{ "_id": "1108", "_from": 3, "_to": 4 },{ "_id": "1109", "_from": 3, "_to" ": 4},{"_id": "1110", "_from": 3, "_to": 4 },{ "_id": "1111", "_from": 3, "_to": 4 }

])

मैं जो हासिल करना चाहता हूं वह कुछ ऐसा है

{
 nodes:[
 {
 "_id" : 3,
 "name": "abcd"
 },
 
 {
 "_id" : 4,
 "name": "abcd"
 }
 ] ,
 hirarchies : [
 {
 "_id" : 3,
 "Hierachy" : [
 {
 "_id" : "1106",
 "_from" : 3.0,
 "_to" : 1.0,
 "depth" : NumberLong(0)
 },
 {
 "_id" : "1107",
 "_from" : 3.0,
 "_to" : 2.0,
 "depth" : NumberLong(0)
 } ] },
 {
 "_id" : 4,
 "Hierachy" : [
 {
 "_id" : "1106",
 "_from" : 3.0,
 "_to" : 1.0,
 "depth" : NumberLong(0)
 } ] } ]

मैंने एक प्रश्न लिखा है लेकिन यह आवश्यक परिणाम नहीं देता है। ग्राफलुकअप का उपयोग करके इसे कैसे करें। मेरी कोशिश की गई क्वेरी है

 db.users.aggregate([
 { $graphLookup: {
 from: "users_hirarchy",
 connectToField: "_from",
 startWith: "$_id",
 maxDepth: 0,
 connectFromField: "_to",
 depthField: "depth",
 as: "hirarchy"
 } } ,
 {$project:{"Data":{
 "id":"$_id",
 "name":"$name"},
  
"Hierachy":"$hirarchy"}
 }])

मैं अपना वांछित आउटपुट कैसे प्राप्त कर सकता हूं?

0
Wajih 19 मार्च 2021, 10:05

1 उत्तर

सबसे बढ़िया उत्तर

इसलिए मैंने इसे समग्र रूप से पहलू का उपयोग करके हासिल किया

db.users.aggregate([
{ $graphLookup: {
from: "users_hirarchy",
connectToField: "_from",
startWith: "$_id",
maxDepth: 0,
connectFromField: "_to",
depthField: "depth",
as: "hirarchy"
}
},  {
    $facet: {
        "Node": [
        {
          $project: {
            "hirarchy": 0
          }
        }],
      "hirarchy": [
        {
          $project: {
            "hirarchy": 1,
            "_id": 1
          }
        }

]}}
])
0
Wajih 19 मार्च 2021, 15:50