添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account Aggregation $graphlookup is not giving proper response in nodejs but working in mongoplayground #12673 Aggregation $graphlookup is not giving proper response in nodejs but working in mongoplayground #12673 vamsideepak opened this issue Nov 10, 2022 · 2 comments

No response

Issue

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.
Nodejs Method:

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);
 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);

Here is Mongo Play ground link
https://mongoplayground.net/p/M4pEB34Ozib

But what I am getting in nodejs is

"_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": []
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
  name: String,
  path: String,
  parentFolder: {type: mongoose.Schema.Types.ObjectId, ref: 'Test'}
});
const Test = mongoose.model('Test', testSchema);
async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();
  const otherEntry = new Test({
    name: 'Folder1',
    path: 'files/Folder1'
  });
  otherEntry.parentFolder = otherEntry._id;
  await otherEntry.save();
  const entry = await Test.create({
    name: 'Folder2',
    path: 'files/Folder2',
    parentFolder: otherEntry._id
  });
  const doc = await Test.create({
    name: 'Folder3',
    path: 'files/Folder3',
    parentFolder: entry._id
  });
  await Test.create({
    name: 'images',
    path: 'files/images',
    parentFolder: otherEntry._id
  });
  await Test.create({
    name: 'images',
    path: 'files/images',
    parentFolder: doc._id
  });
  const res = await Test.aggregate([{$match: { parentFolder: {$exists: true}}}, { $graphLookup: {
    from: 'Test',
    startWith: '$_id',
    connectFromField: 'parentFolder',
    connectToField: 'parentFolder',
    maxDepth: 1,
    depthField: 'depth',
    as: "TreeResult"
  }}]);
  // mongoose.connection.db.collection.aggregate()
  console.log(res);
run();

@vamsideepak this is expected behavior, because mongoose.model('FileFolder', fileFolderSchema) means the collection is 'filefolders', not 'FileFolder'. You need to use the collection name, not the model name, for aggregations. Try the following instead.

const treeStructure =  await FileFolder.aggregate([
                $match: {
                  parentFolder: {
                    $exists: true
                $graphLookup: {
                  from: FileFolder.collection.name, // <-- use the collection name instead of the model name
                  startWith: "$_id",
                  connectFromField: "parentFolder",
                  connectToField: "parentFolder",
                  maxDepth: 1,
                  depthField: "depth",
                  as: "TreeResult"
              ]);
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Nov 30, 2022