MongoDB : Update document array value object [duplicate] - mongodb

This question already has answers here:
How to Update Multiple Array Elements in mongodb
(16 answers)
Closed 5 years ago.
I have document like below
{
_id="12345",
details:[
{
name:"Customer_Name",
time:"request_time"
},
{
name:"Customer_Name",
time:"request_time"
},
{
name:"Customer_Name",
time:"request_time"
}, ....
]
}
I want to update "name" field in all the objects of "details" array.
I can use it update each array object using
db.customer.updateMany({_id:"12345"},{$set:{"details.0.name":"My_Name"}});
Is there any way to update all of them at once

Try:
db.customer.find({ _id:"12345" })
.forEach(function (doc) {
doc.details.forEach(function (details) {
details.name="My_Name";
});
db.customer.save(doc);
});

Related

How to update each document with one query in mongodb [duplicate]

This question already has an answer here:
How to update each value with one query in mongodb
(1 answer)
Closed 3 years ago.
I have a data as below.
// first document
{
"id": 1,
"exist": true
}
// second document
{
"id": 2,
"exist": false
}
// third document
{
"id": 3,
"exist": false
}
When I findOneAndUpdate({_id:2}),{exist:true}), I hope that exist of 'id:1' is changed to false automatically in one query using aggregate or etc.
could you recommend some idea for it? Thank you so much for reading my question.
I don't really understand what do you want to update in your collection but to update many document use updateMany :
db.collection.updateMany(
{ violations: { $gt: 1 } }, // the filter to select the wanted document
{ $set: { "exist" : true } } // the change to apply to the field
);

How to return only the updated subdocument inside of array after an update [duplicate]

This question already has answers here:
Mongoose select fields to return from findOneAndUpdate
(7 answers)
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 3 years ago.
I have a user model with the following schema
{
_id: userId,
inbox: {
inbox: [Array of message documents],
sent: [Array of message documents],
}
}
Is there a way to get only the updated subdocument after an update in mongodb / mongoose ?
The update is good i just want to return the updated subdocument instead of querying the db again
db.getCollection("users").findOneAndUpdate(
{ "inbox.sent._id": ObjectId("5cea23be8c03c800d43a8376") },
{ "$set": { "inbox.sent.$.inTrash": false } },
{ "inbox.sent.$": 1 } <-- how to return only the subdocument like in a query
);
expected output the array at inbox.sent with only the updated doc
{
_id: userId,
inbox: {
sent: [{ updated doc}]
}
}
You need to apply a callback to the findOneAndUpdate function
e.g:
db.getCollection("users").findOneAndUpdate(
{ "inbox.sent._id": ObjectId("5cea23be8c03c800d43a8376") },
{ "$set": { "inbox.sent.$.inTrash": false } },
{ "inbox.sent.$": 1 },
function (err, res) {
//here you have the res, which will be the affected record
}
);

Remove element of internal field array MongoDb [duplicate]

This question already has answers here:
MongoDB, remove object from array
(7 answers)
Closed 4 years ago.
I have the following situation:
{ code: 0, array:[{ _id: 1, ....}, { _id: 2, ....}, { _id: 18, ....}]}
I need to remove the element inside 'array' field matching the '_id' field.
How can I do that?
Thank you.
Use $update & $pull:
db.yourCollection.update(
{ }, // <-- your selection criteria
{ $pull: { array: { _id: 2 } } // <-- what you want to remove from which field
)

MongoDB update nested array of object by index [duplicate]

This question already has answers here:
Update field in exact element array in MongoDB
(5 answers)
Closed 5 years ago.
Let's suppose to there is db like below...
{ _id: 1234,
key: 'Contacts',
value: [
{ name: 'McDonald', phone: '1111'},
{ name: 'KFC', phone: '2222'}
]
}
And I want to change KFC's phone number to '3333'.
What I did is
DB.findOne({ key: 'Contacts' }, function(err, db){
db.value[1]['phone'] = '3333'
db.save(function(err, result){
// done
})
}
)
But it didn't update the database. What am I wrong?
There's no specific _id in elements of array for some reason.
Only the way to find specific element is index.
Use the positional operator $
more info : https://docs.mongodb.com/manual/reference/operator/update/positional/#up.S
DB.update({key: "Contacts", "value.name": "KFC" },
{ $set: { "value.$.phone" : 666 } },function(err,doc){
});

Increment all element in nested array [duplicate]

This question already has answers here:
How to Update Multiple Array Elements in mongodb
(16 answers)
Closed 8 years ago.
I have a document like that :
{
_id: ".....",
messages: [
{
....
votes: 2
},
{
....
votes: 2
}
]
}
I would like to increment ALL votes field in the array in the same request.
How to do that ? The $ operator select only the first element.
Thank you !
You might have to do this with some JS loops:
db.collection.find( { "_id": SOME_ID } ).forEach( function( doc ) {
for ( i in doc.messages ){
doc.messages[ i ].votes += 1;
}
db.collection.save( doc );
} );
What I'm doing here is iterating over each document in the cursor and iterating over it's messages property incrementing each element's votes attribute by one.