【问题标题】:how update the values of array inside other array on mongodb?如何更新MongoDB上其他数组中的数组值?
【发布时间】:2019-03-04 15:11:03
【问题描述】:

我需要帮助,我正在尝试更新 content 数组内部的数据 stage 数组:

{
  "_id": ObjectId("5a95b1581ddd0d2c60b90612"),
  "country": "Chile",
  "name": "Xiaomi",
  "start": new Date("2018-02-28T00:00:00-0300"),
  "end": new Date("2018-03-03T00:00:00-0300"),
  "budget": 5000000,
  "instructions": "efaw",
  "status": null,
  "createdAt": new Date(1519568976029),
  "budgetAccum": 2465914.3070999998599,
  "brief": {
    "stage": [
      {
        "cantPost": 1,
        "cantStories": 1,
        "desc": "Descripción",
        "title": "Title of etapa",
        "_id": ObjectId("5bad2e6814bc8ae0450d79a1"),
        "content": [
          {
            "id": 1,
            "desc": "Descripción contenido",
            "hashtions": "#hashtag / @mentions",
            "media": "photo or video",
            "participants": [

            ],
            "type": "post or history",
            "title": "Title of contenido"
          }
        ]
      }
    ]
  }
}

我的查询是这样的:

const updateContent = await this.update({
    "_id": ObjectId(campaignId),
    "brief.stage._id": ObjectId(data.id),
    "brief.stage.content.id": data.idContent
}, {
    "$set": {
        "brief.stage.content.$.desc": data.content.desc,
        "brief.stage.content.$.hashtions": data.content.hashtions,
        "brief.stage.content.$.media": data.content.media,
        "brief.stage.content.$.participants": data.content.participants,
        "brief.stage.content.$.type": data.content.type,
        "brief.stage.content.$.title": data.content.title,

    }
});

错误是这样的:

{ “数据”: {}, "message": "不能使用部分(stage of brief.stage.content.0.desc)遍历元素({stage: [ { cantPost: 1, cantStories: 1, desc: \"Descripción\", title: \"etapa 的标题\", _id: ObjectId('5bad2e6814bc8ae0450d79a1'), content: [ { id: 1, desc: \"Descripción contenido\", hashtions: \"#hashtag / @mentions\", media: \"照片或视频\",参与者:[],类型:\"post or history\",标题:\"Contenido 标题\"}]},{ cantPost: 1, cantStories: 1, desc: \"Descriptción\" , title: \"etapa 的标题\", _id: ObjectId('5bad3932206981e377a31971'), content: [ { hola: \"true\", adios: \"false\" } ] } ]})", "name": "活动引擎", "type": "BAD_REQUEST", “状态码”:400

任何解释,我该如何解决?

谢谢

【问题讨论】:

  • 你的mongodb版本是多少?你可以在这里使用arrayFilters
  • 版本是这个^4.1.2

标签: node.js mongodb


【解决方案1】:

在mongodb 3.6及以上版本中可以使用arrayFilters

db.getCollection('testcollections').update(
  { "_id" : ObjectId("5a95b1581ddd0d2c60b90612") },
  { "$set" : {
    "brief.stage.$[e1].content.$[e2].desc" : 1,
    "brief.stage.$[e1].content.$[e2].hashtions" : 2,
    "brief.stage.$[e1].content.$[e2].media" : 3,
    "brief.stage.$[e1].content.$[e2].participants" : 4,
    "brief.stage.$[e1].content.$[e2].type" : 5,
    "brief.stage.$[e1].content.$[e2].title" : 6
  }},
  { "arrayFilters": [
    { "e1._id": ObjectId("5bad2e6814bc8ae0450d79a1") },
    { "e2.id": 1 }
  ]}
)

【讨论】:

  • 在这个查询中:e1和e2是索引吗?响应是 "data": { "ok": 0, "n": 0, "nModified": 0 }
  • 不,它们不是索引。它们是标识符docs.mongodb.com/manual/reference/operator/update/…
  • 用 mongo shell 测试。我的回复WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  • 代码返回 "data": { "ok": 0, "n": 0, "nModified": 0 }
  • 你的id转换成objectId了吗?
猜你喜欢
  • 1970-01-01
  • 2011-03-02
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
相关资源
最近更新 更多