Search code examples
node.jsmongodbmongoosegraphlookup

$graphLookup return void array


I have below collection:

[{
    "_id":  "60035d0a1599912a5c814e58",
    "idUsuario": "600365521599912a5c814e5e",
    "parentNode": "",
    "piernaPadre": "",
    "estado": "1"
    },
    {
    "_id": "6003827b06b4423c9ca7e6aa",
    "idUsuario": "60036e53dda7df34749ebf3a",
    "parentNode": "60035d0a1599912a5c814e58",
    "piernaPadre": "d",
    "estado": 1
    },
    {
    "_id": "60038c92ea7d593fe029cc0f",
    "idUsuario": "600382a506b4423c9ca7e6ab",
    "parentNode": "6003827b06b4423c9ca7e6aa",
    "piernaPadre": "d",
    "estado": 1
}]

I need to get the descendants of a node, I'm trying with $graphLookup,

$graphLookup: {
          from: "nodoModel",
          startWith: "$_id",
          connectFromField: "_id",
          connectToField: "parentNode",
          as: "arrayDes"
        }

but does not work, the return is void. Is there a mistake?

Thanks.

EDIT 1

Now I can get a result when try to get de ancestors of a node:

 $graphLookup: {
          from: "nodos",
          startWith: "$_id",
          connectFromField: "_id",
          connectToField: "nodoPadre",
          as: "padre"
        }

Whit below result:

[
    {
    _id: 60035d0a1599912a5c814e58,
    idUsuario: '600365521599912a5c814e5e',
    parentNode: '',
    piernaPadre: '',
    estado: '1',
    padre: [ [Object] ]
    },
    {
    _id: 6004589436a40941f48121f8,
    idUsuario: '600365e9ccf1e51b2cab341f',
    parentNode: '60035d0a1599912a5c814e58',
    piernaPadre: 'd',
    estado: 1,
    createdAt: 2021-01-17T15:32:36.986Z,
    updatedAt: 2021-01-17T15:32:36.986Z,
    __v: 0,
    padre: [ [Object] ]
    },
    {
    _id: 6004592936a40941f48121fa,
    idUsuario: '6004591536a40941f48121f9',
    parentNode: '6004589436a40941f48121f8',
    piernaPadre: 'd',
    estado: 1,
    createdAt: 2021-01-17T15:35:05.626Z,
    updatedAt: 2021-01-17T15:35:05.626Z,
    __v: 0,
    padre: [ [Object] ]
    }
]

But I need to get the descendants not the ancestors

EDIT 2

(in the original model parentNode is named nodoPadre)

It is a screenshot of my code: enter image description here

and that is the console.log: enter image description here

arrayDes is a void array. I'm using mongoose, maybe it is related with the problem?

EDIT 3

I'm trying to change type of nodoPadre to objectId enter image description here


Solution

  • Try query to get descendants,

    • $match put condition
    • $grouphLookup same as yours
      { $match: { nodoPadre: "" } },
      {
        $graphLookup: {
          from: "collection",
          startWith: "$_id",
          connectFromField: "_id",
          connectToField: "nodoPadre",
          as: "arrayDes"
        }
      }
    

    Playground


    For your Second Edit:

    Update nodoPadre type to object id type in all documents, and change type in schema to type: mongoose.Types.ObjectId

    nodoPadre: {
      type: mongoose.Types.ObjectId,
      required: [false, '2'],
      index: true
    }