I have Locations collection in MongoDB that includes two documents.
Each one of them has its own array of confirmed bookings as objects with Ids.
How can I update the object that has _id: c1?
Is it possible to do that in a single query?
[{
_id :63233022222222,
title :"Ravenholm",
price: 180,
capacity: 2,
confirmedBookings: [
{
_id : r1,
start: 1663207200000,
finish: 1663210799999
},
{
_id: r2,
start: 1663322400000,
finish: 1663329599999
}
]
},
{
_id :6323301111111111,
title :"City 17",
price: 360,
capacity: 4,
confirmedBookings: [
{
_id : c1,
start: 1663207200000,
finish: 1663210799999
},
{
_id: c2,
start: 1663322400000,
finish: 1663329599999
}
]
}]
2
Answers
You can use elemMatch to find the element inside array, then update it’s value.
Write your schema’s name to documentName.
Also, write your value to search as $elemMatch: {_id: "c1"} . The newValue is the new value you want to update.
I hope, this helps you. Good luck.
You should be able to query and update the specific object with: