使用猫鼬更新嵌套数组中的对象

时间:2019-05-28 13:08:03

标签: arrays node.js mongodb collections mongoose-schema

嗨,我是mongoDB的新手,我想更新一个嵌套数组,有人可以帮忙吗

json:

[
    {
        "_id": "5cecc744ed114519ecb0b309",
        "title": "Feature_1",
        "description": "feature 1 description",
        "scenarios": [
            {
                "steps": [],
                "_id": "5cece3568d1a4525ac3c30ca",
                "title": "Scenario_1",
                "description": "scenario 1 description"
            },
            {
                "steps": [],
                "_id": "5cecf0772bafd629f848b686",
                "title": "Scenario 1",
                "description": "scenario 1 description"
            },
            {
                "steps": [],
                "_id": "5cecf08d2bafd629f848b689",
                "title": "Scenario_2",
                "description": "scenario 2 description"
            }
        ]
    },
    {
        "_id": "5cecf1667cd5aa2680a505c7",
        "title": "Feature_2",
        "description": "feature 2 description",
        "scenarios": [],
        "__v": 0
    }
]

我需要使用猫鼬在Feature_1的方案_1中插入/更新“ step”数组

使用下面的代码得到它,谢谢:)

const feature_t_1 = await schema.Feature.update(
      {
        'title': featureTitle,
        'scenarios': {
          "$elemMatch": {
            "title": scenarioTitle,//"array2._id": "123"
          }
        }
      },
      {
        "$push": { "scenarios.$[inner].steps": step }
      },
      {
        "arrayFilters": [{ "inner.title": scenarioTitle }] 
      }
    )

0 个答案:

没有答案