1

Here i want to write the update query like inside tags array suppose value is good i want to change very-good.i had written but is not working properly,suppose insdie tags two value is there means , two values itself getting updated

my documents

    /* 1 createdAt:12/18/2018, 5:59:33 PM*/
{
    "_id" : ObjectId("5c18e82da073cdc5c70072dc"),
    "roleID" : "3",
    "tags" : [
        "good"
    ]
},

/* 2 createdAt:12/18/2018, 5:59:33 PM*/
{
    "_id" : ObjectId("5c18e82da073cdc5c70072db"),
    "roleID" : "2",
    "tags" : [
        "very-good",
        "good"
    ]
},
3
  • what you get after run ther query? Commented Dec 18, 2018 at 13:19
  • Is options-short always in the first position? Commented Dec 18, 2018 at 13:21
  • @Prasanna I have added a simple solution for you, using updateMany instead of update with {"multi": true} Commented Dec 18, 2018 at 13:35

3 Answers 3

1

This code worked perfectly, it uses updateMany instead of just update. This code uses the $ operator:

Code

db.QuestionContents.updateMany({contentID: {$in: contentID}, tags: "options-short"}, {$set: {"tags.$": "options-2x2"}});

More info in the docs

Output

Here is the result of db.QuestionContents.find({}):

Output

Sign up to request clarification or add additional context in comments.

1 Comment

@Prasanna if my answer worked for you, please, mark it as accepted (green checkmark). Thnx!
0

Use this query to update -

db.QuestionContents.update({contentID: {$in: contentID}, tags: "options-short" }, {$set:{"tags.$":"options-short2*2"}},{"multi": true});

Comments

0

use $addToSet with $each

db.QuestionContents.update({contentID: {$in: contentID} }, {$addToSet:{tags:{
  $each:["options-2x2"]}}},{"multi": true});

1 Comment

it is not updating, it is inserting the inside the array, it has to update

Your Answer

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