1

I need help, I am trying to update the data inside of the array content inside of the stage array:

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

my Query is this:

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,

    }
});

And the error is this:

{ "data": {}, "message": "cannot use the part (stage of brief.stage.content.0.desc) to traverse the element ({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\" } ] }, { cantPost: 1, cantStories: 1, desc: \"Descripción\", title: \"Title of etapa\", _id: ObjectId('5bad3932206981e377a31971'), content: [ { hola: \"true\", adios: \"false\" } ] } ]})", "name": "Campaign-engine", "type": "BAD_REQUEST", "statusCode": 400

Any explanation and how could I solve it?

Thanks

2
  • What is your mongodb version? you can use arrayFilters here Commented Sep 28, 2018 at 15:37
  • the versions is this ^4.1.2 Commented Sep 28, 2018 at 15:40

1 Answer 1

1

You can use arrayFilters in mongodb 3.6 and above

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 }
  ]}
)
Sign up to request clarification or add additional context in comments.

6 Comments

In this query: e1 and e2 is the index? the response is "data": { "ok": 0, "n": 0, "nModified": 0 }
no they are not index. they are identifiers docs.mongodb.com/manual/reference/operator/update/…
Test with mongo shell. My response WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
the code return "data": { "ok": 0, "n": 0, "nModified": 0 }
Have you converted your id to objectId ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.