mongoose $graphLookup返回空数组

xmd2e60i  于 2022-11-13  发布在  Go
关注(0)|答案(2)|浏览(288)

我有下面的收藏:

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

我需要获取一个节点的后代,我尝试使用$graphLookup

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

但不起作用,退货无效。是否有错误?

  • 谢谢-谢谢

编辑1

现在我可以得到一个结果,当试图得到一个节点的祖先:

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

白下面的结果:

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

但我需要的是后代而不是祖先

编辑2

(in原始模型的parentNode命名为nodoPadre)
这是我的代码截图:

也就是console.log:

arrayDes是一个空数组,我用的是mongoose,可能和问题有关吧?

编辑3

我正在尝试将nodoPadre的类型更改为objectId

x3naxklr

x3naxklr1#

尝试查询以获取后代,

  • $match放置条件
  • $grouphLookup和你的一样
{ $match: { nodoPadre: "" } },
  {
    $graphLookup: {
      from: "collection",
      startWith: "$_id",
      connectFromField: "_id",
      connectToField: "nodoPadre",
      as: "arrayDes"
    }
  }

Playground
对于第二次编辑:
将所有文档中的nodoPadre类型更新为对象ID类型,并将架构中的类型更改为type: mongoose.Types.ObjectId

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

nfeuvbwi2#

我遇到了同样的问题,最后我通过将集合名称改为小写字母来解决它,它应该是基于mongodb的复数。
Mongoose模式:

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

Nodejs方法:

folder_tree_structure: async(ctx:any) => {
        try {
          let treeStructure =  await FileFolder.aggregate([
            {
                $match: {
                  parentFolder: {
                    $exists: true
                  }
                }
              },
              {
                $graphLookup: {
                  from: 'filefolders', // here your collection name is always small letters and plural
                  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.status = 400;
            ctx.body = 'Not found' 
        }
    }

我的节点响应:

[
    {
        "_id": "636cd1d2692344da47f3e2f0",
        "name": "Folder2",
        "path": "files/Folder2",
        "metadata": {
            "version": 1,
            "created_by": "user-1",
            "created_on": "2022-11-10T10:26:26.828Z",
            "timeout": -1
        },
        "__v": 0,
        "parentFolder": "636cd213692344da47f3e2f5",
        "TreeResult": [
            {
                "_id": "636cd238692344da47f3e301",
                "name": "Folder3",
                "path": "files/Folder3",
                "metadata": {
                    "version": 1,
                    "created_by": "user-1",
                    "created_on": "2022-11-10T10:28:08.655Z",
                    "timeout": -1
                },
                "__v": 0,
                "parentFolder": "636cd1d2692344da47f3e2f0",
                "depth": 0
            },
            {
                "_id": "636d075874867ade3d3c3224",
                "name": "images",
                "path": "files/images",
                "metadata": {
                    "version": 1,
                    "created_by": "user-1",
                    "created_on": "2022-11-10T14:14:48.871Z",
                    "timeout": -1
                },
                "__v": 0,
                "parentFolder": "636cd1d2692344da47f3e2f0",
                "depth": 0
            }
        ]
    },
    {
        "_id": "636cd213692344da47f3e2f5",
        "name": "Folder1",
        "path": "files/Folder1",
        "metadata": {
            "version": 1,
            "created_by": "user-1",
            "created_on": "2022-11-10T10:27:31.600Z",
            "timeout": -1
        },
        "__v": 0,
        "parentFolder": "636cd213692344da47f3e2f5",
        "TreeResult": [
            {
                "_id": "636cd213692344da47f3e2f5",
                "name": "Folder1",
                "path": "files/Folder1",
                "metadata": {
                    "version": 1,
                    "created_by": "user-1",
                    "created_on": "2022-11-10T10:27:31.600Z",
                    "timeout": -1
                },
                "__v": 0,
                "parentFolder": "636cd213692344da47f3e2f5",
                "depth": 0
            },
            {
                "_id": "636cd1d2692344da47f3e2f0",
                "name": "Folder2",
                "path": "files/Folder2",
                "metadata": {
                    "version": 1,
                    "created_by": "user-1",
                    "created_on": "2022-11-10T10:26:26.828Z",
                    "timeout": -1
                },
                "__v": 0,
                "parentFolder": "636cd213692344da47f3e2f5",
                "depth": 0
            },
            {
                "_id": "636ce2452e97522606b14b23",
                "name": "images",
                "path": "files/images",
                "metadata": {
                    "version": 1,
                    "created_by": "user-1",
                    "created_on": "2022-11-10T11:36:37.691Z",
                    "timeout": -1
                },
                "__v": 0,
                "parentFolder": "636cd213692344da47f3e2f5",
                "depth": 0
            }
        ]
    },
    {
        "_id": "636cd238692344da47f3e301",
        "name": "Folder3",
        "path": "files/Folder3",
        "metadata": {
            "version": 1,
            "created_by": "user-1",
            "created_on": "2022-11-10T10:28:08.655Z",
            "timeout": -1
        },
        "__v": 0,
        "parentFolder": "636cd1d2692344da47f3e2f0",
        "TreeResult": []
    },
    {
        "_id": "636ce2452e97522606b14b23",
        "name": "images",
        "path": "files/images",
        "metadata": {
            "version": 1,
            "created_by": "user-1",
            "created_on": "2022-11-10T11:36:37.691Z",
            "timeout": -1
        },
        "__v": 0,
        "parentFolder": "636cd213692344da47f3e2f5",
        "TreeResult": []
    },
    {
        "_id": "636d075874867ade3d3c3224",
        "name": "images",
        "path": "files/images",
        "metadata": {
            "version": 1,
            "created_by": "user-1",
            "created_on": "2022-11-10T14:14:48.871Z",
            "timeout": -1
        },
        "__v": 0,
        "parentFolder": "636cd1d2692344da47f3e2f0",
        "TreeResult": []
    }
]

这里是蒙戈平台地链接:https://mongoplayground.net/p/M4pEB34Ozib

相关问题