通过mongodb聚合获取父子关系

时间:2019-05-23 06:34:54

标签: node.js mongodb express mongoose aggregation-framework

我有一个名为“ location”的收藏。在这个集合中,所有子集合和父集合都是商店。现在,我想创建一个查询,使我返回父级,并以逗号分隔的子字符串。

收藏

businessId: { type: mongoose.Schema.Types.ObjectId, ref: 'admin' },
parentId: { type: mongoose.Schema.Types.ObjectId, ref: 'location' },
name: { type: String },
image: { type: String },
imageManipulation: { type: String },
locationColor: [{ range: { type: String }, color: { type: String } }],
area: {},
settings: {},
status: { type: String, enum: [0, 1], default: 1 },
isChild: { type: String, enum: [0, 1] },
parentPosition: { type: String }

在上面的集合中,您可以看到parentId字段。如果该位置是孩子,则该位置为parentId。如果该位置是父级,则parentId将为null。父级位置可以是N级子级位置。

收集数据

[{
    "_id": ObjectId("5ce4f84547e90a0b9c3c4763"),
    "name": "Test",
    "settings": {
        "zoom": "3",
        "positionX": "69",
        "positionY": "69",
        "width": "500",
        "height": "334"
    },
    "parentId": null,
    "image": "1558509637101.jpg",
    "status": "0",
    "businessId": ObjectId("5cbd61dc3b56b902284ea388"),
    "locationColor": [],
    "updatedAt": ISODate("2019-05-22T12:59:26.013Z"),
    "createdAt": ISODate("2019-05-22T07:20:37.112Z"),
    "__v": 0
},

{
    "_id": ObjectId("5ce50caf09359e1b8ccf5c79"),
    "name": "Test sub 1",
    "settings": {
        "zoom": "3",
        "positionX": "48",
        "positionY": "3",
        "width": "500",
        "height": "334"
    },
    "area": "",
    "parentId": ObjectId("5ce4f84547e90a0b9c3c4763"),
    "image": "1558514863396.jpg",
    "status": "0",
    "businessId": ObjectId("5cbd61dc3b56b902284ea388"),
    "locationColor": [],
    "updatedAt": ISODate("2019-05-22T12:59:21.883Z"),
    "createdAt": ISODate("2019-05-22T08:47:43.421Z"),
    "__v": 0
},
{
    "_id": ObjectId("5ce53977e46da33e6cfdd9d1"),
    "name": "Test Sub 2",
    "settings": {
        "zoom": "5",
        "positionX": "0",
        "positionY": "0",
        "width": "500",
        "height": "334"
    },
    "area": "",
    "parentId": ObjectId("5ce50caf09359e1b8ccf5c79"),
    "image": "1558526327126.jpg",
    "businessId": ObjectId("5cbd61dc3b56b902284ea388"),
    "locationColor": [],
    "updatedAt": ISODate("2019-05-22T11:58:47.147Z"),
    "createdAt": ISODate("2019-05-22T11:58:47.147Z"),
    "__v": 0
}]
  

预期结果

测试,测试子1,测试子2

  

JSON的预期结果

[{
    "_id": ObjectId("5ce4f84547e90a0b9c3c4763"),
    "name": "Test",
},
{
    "_id": ObjectId("5ce50caf09359e1b8ccf5c79"),
    "name": "Test, Test sub 1",
},
{
    "_id": ObjectId("5ce53977e46da33e6cfdd9d1"),
    "name": "Test, Test sub 1, Test Sub 2",
}]

1 个答案:

答案 0 :(得分:1)

您基本上需要$graphLookup才能在同一集合上进行递归循环。

db.location.aggregate([
  { "$graphLookup": {
    "from": "location",
    "startWith": "$parentId",
    "connectFromField": "parentId",
    "connectToField": "_id",
    "as": "parent"
  }},
  { "$project": {
    "name": {
      "$concat": [
        "$name",
        { "$reduce": {
          "input": "$parent",
          "initialValue": "",
          "in": { "$concat": [",", "$$this.name", "$$value"] }
        }}
      ]
    }
  }}
])

将会输出

[
  {
    "_id": ObjectId("5ce4f84547e90a0b9c3c4763"),
    "name": "Test"
  },
  {
    "_id": ObjectId("5ce50caf09359e1b8ccf5c79"),
    "name": "Test sub 1,Test"
  },
  {
    "_id": ObjectId("5ce53977e46da33e6cfdd9d1"),
    "name": "Test Sub 2,Test sub 1,Test"
  }
]

MongoPlayground