Click here to Skip to main content
15,565,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using $graphlookup with mongoose , it is working fine in mongoplay ground but in nodejs it is not giving tree structure data. Instead it is giving
TreeResult
as empty array
Please find the below details:
below is the my method in nodejs
JavaScript
<pre>    folder_tree_structure: async(ctx:any) => {
        try {
          const treeStructure =  await FileFolder.aggregate([
            {
                $match: {
                  parentFolder: {
                    $exists: true
                  }
                }
              },
              {
                $graphLookup: {
                  from: "FileFolder",
                  startWith: "$_id",
                  connectFromField: "parentFolder",
                  connectToField: "parentFolder",
                  maxDepth: 1,
                  depthField: "depth",
                  as: "TreeResult"
                }
              }
              ]);
            console.log('tree structure',treeStructure )
            ctx.status = 200;
            ctx.body = treeStructure;
        } catch (err) {
            ctx.response.status(500).send(err);
        }
    }

and my mongoose schema:
const fileFolderSchema = new Schema({
    name: String,
    parentFolder: {type: Mongoose.Types.ObjectId, ref: "FileFolder"},
    path: String,
    metadata: {
        type: metaDataSchema
    }
}, { id: false }).set('toJSON', {
    virtuals: true
});

export const FileFolder = Mongoose.model('FileFolder', fileFolderSchema);


What I have tried:

Here you can find what I have tried in mongo playground:

Mongo playground[^]

and the output when I tried in nodejs
[
    {
        "_id": "6360468045689c3d0c70e53c",
        "name": "Folder1",
        "path": "files/Folder1",
        "metadata": {
            "version": 1,
            "created_by": "user-1",
            "created_on": "2022-10-31T22:04:48.233Z"
        },
        "__v": 0,
        "parentFolder": "6360468045689c3d0c70e53c",
        "TreeResult": []
    },
    {
        "_id": "6360468d45689c3d0c70e558",
        "name": "Folder2",
        "path": "files/Folder2",
        "metadata": {
            "version": 1,
            "created_by": "user-1",
            "created_on": "2022-10-31T22:05:01.787Z"
        },
        "__v": 0,
        "parentFolder": "6360468045689c3d0c70e53c",
        "TreeResult": []
    },
    {
        "_id": "6360469445689c3d0c70e55c",
        "name": "Folder3",
        "path": "files/Folder3",
        "metadata": {
            "version": 1,
            "created_by": "user-1",
            "created_on": "2022-10-31T22:05:08.309Z"
        },
        "__v": 0,
        "parentFolder": "6360468d45689c3d0c70e558",
        "TreeResult": []
    },
    {
        "_id": "636a732946b670e689afd454",
        "name": "images",
        "path": "files/images",
        "metadata": {
            "version": 1,
            "created_by": "user-1",
            "created_on": "2022-11-08T15:18:01.190Z",
            "timeout": -1
        },
        "__v": 0,
        "parentFolder": "6360468045689c3d0c70e53c",
        "TreeResult": []
    },
    {
        "_id": "636a735f46b670e689afd46d",
        "name": "images",
        "path": "files/images",
        "metadata": {
            "version": 1,
            "created_by": "user-1",
            "created_on": "2022-11-08T15:18:55.764Z",
            "timeout": -1
        },
        "__v": 0,
        "parentFolder": "6360468d45689c3d0c70e558",
        "TreeResult": []
    }
]
Posted
Updated 10-Nov-22 0:45am
v3
Comments
Richard Deeming 10-Nov-22 5:42am    
If it's "not working" and "failing to run" in node, then how are you able to get output from node?

Update your question to provide a clear and complete description of the problem. Include the full details of any errors.
Vamsi Deepak 10-Nov-22 5:48am    
Sorry richard . This is my first question code project. I have updated my question please have a look . In mongo play ground the response is proper (you can check the link that I provided ) but when I run the same piece of code it is giving TreeResult as empty array.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900