0

below is my collection in Mongo dB

[{
    "_id": "5d1c3f9384d8c520c0d647d0",
    "userName": "test@outlooks.com",
    "password": "$2b$10$TM8MLk1VIl1PHaBNBG.EK.Maj.UjdI1r1M.s4THSM5wYkKnWKU82a",
    "firstName": null,
    "lastName": null,
    "cards": [{
        "cardName": "card-1",
        "stage1": [],
        "stage2": [],
        "lastExamDate": null,
        "createdDate": "2019-07-03T06:55:45.890Z"
    }, {
        "cardName": "card-2",
        "stage1": [],
        "stage2": [],
        "lastExamDate": null,
        "createdDate": "2019-07-03T07:14:32.278Z"
    }]
},
{
    "_id": "384d8f9384d8c520c0d333m9",
    "userName": "sample@gmail.com",
    "password": "$kKn10$TM8MLk1VIl1PHaBNBG.EK.Maj.UjdI1r1M.s4THSM5wYkKnWKUkKn",
    "firstName": user1,
    "lastName": last1,
    "cards": [{
        "cardName": "English",
        "stage1": [],
        "stage2": [],
        "lastExamDate": null,
        "createdDate": "2019-07-03T06:55:45.890Z"
    }, {
        "cardName": "Spanish",
        "stage1": [],
        "stage2": [],
        "lastExamDate": null,
        "createdDate": "2019-07-03T07:14:32.278Z"
    }]
}
]

here is what Im trying to do in mongo dB with Node.js
User want to update the cardName from card-1 to Germany.

as you see cardName is an object inside an array.

1- I have to find the _id
2- then I have to find the cardName he want to update.
3- update the cardName with the new one

I couldn't figure it out how to do that.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Jubin
  • 71
  • 1
  • 5

3 Answers3

0

Use find to get the correct index of the object (no need for _id), then use find to get the correct index of the card object, then update that field.

let arr = [{"_id":"5d1c3f9384d8c520c0d647d0","userName":"test@outlooks.com","password":"$2b$10$TM8MLk1VIl1PHaBNBG.EK.Maj.UjdI1r1M.s4THSM5wYkKnWKU82a","firstName":null,"lastName":null,"cards":[{"cardName":"card-1","stage1":[],"stage2":[],"lastExamDate":null,"createdDate":"2019-07-03T06:55:45.890Z"},{"cardName":"card-2","stage1":[],"stage2":[],"lastExamDate":null,"createdDate":"2019-07-03T07:14:32.278Z"}]},{"_id":"384d8f9384d8c520c0d333m9","userName":"sample@gmail.com","password":"$kKn10$TM8MLk1VIl1PHaBNBG.EK.Maj.UjdI1r1M.s4THSM5wYkKnWKUkKn","firstName":"user1","lastName":"last1","cards":[{"cardName":"English","stage1":[],"stage2":[],"lastExamDate":null,"createdDate":"2019-07-03T06:55:45.890Z"},{"cardName":"Spanish","stage1":[],"stage2":[],"lastExamDate":null,"createdDate":"2019-07-03T07:14:32.278Z"}]}];

const objIdx = arr.findIndex(({ cards }) => cards.some(({ cardName }) => cardName == "card-1"));
const cardObjIdx = arr[objIdx].cards.findIndex(({ cardName }) => cardName == "card-1");

arr[objIdx].cards[cardObjIdx].cardName = "Germany";

console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: auto; }
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • Thanks Jack, but this wont work in different reasons. if there was cards with same name for different users. i want to know how can i update with Mondgo functions, line findOneAndUpdate, or updateOne – Jubin Jul 06 '19 at 00:19
0

here is how we can update

    request.findOneAndUpdate(
        {_id:ObjectId('384d8f9384d8c520c0d333m9')},
        {$set:{'cards.$[elem].cardName':'Germany'}},
        {arrayFilters:[{'elem.cardName':'card-15'}]}
    );
Jubin
  • 71
  • 1
  • 5
0

No Need to find _id, directly update based on cardName. replace db.getCollection('cards') with db.getCollection('your collection name')

db.getCollection('cards').update({"cards":{"$elemMatch":{"cardName": "card-1"}}},{"$set":{"cards.$.cardName": "Germany"}})

Piyush
  • 129
  • 5