通过MongoDB聚集使父母与孩子关系



我有一个称为"位置"的集合。在此收藏中,所有子女收藏馆都是商店。现在,我想创建一个将我的父母归还给孩子逗号分隔字符串的查询。

收集

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将无效。父位置可以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",
}]

您基本上需要 $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

最新更新