This question already has answers here:
MongoDB: How to update multiple documents with a single command?
(13 answers)
Closed 7 years ago.
I have two documents with "Name" values "Raja_5" and "Raja_6".
I have written the following codes to update the city in two documents.
collection.update({"Name":{"$in":["Raja_5","Raja_6"]}},{"$set":{"City":"Hyd"}})
(or)
collection.update({"$or":[{"Name":"Raja_5"},{"Name":"Raja_6"}]},{"$set":{"City":"Hyd"}})
But the document with "Raja_5" is getting updated but not the other document in both the cases.
Please help me.
The following code is working now
collection.update({
"Name": {
"$in": ["Raja_5", "Raja_6"]
}
}, {
"$set": {
"City": "Hyd"
}
}, "false", "true")
This is because the 'multi' operator is not been set.
db.collection.update({"$or":[{"Name":"Raja_5"},{"Name":"Raja_6"}]},{"$set":{"City":"Hyd"}}, { multi: true })
above is the shell command for it.
for pymongo, i think it can be done as below
collection.update({"$or":[{"Name":"Raja_5"},{"Name":"Raja_6"}]},{"$set":{"City":"Hyd"}}, multi=True)
In version 3.0, update_many operation has been introduced with format as :
update_many(filter, update, upsert=False)
http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.update_many
Related
This question already has answers here:
Update MongoDB field using value of another field
(12 answers)
Closed last year.
I am unable to update my document when I run
db.xx.updateOne({_id: ObjectId(""}, {$set: {lq: uq}) I get the error uncaught exception: ReferenceError: uq is not defined
uq field exists and contains integers, not sure why it don't work. Please help.
db.collection.update({
"_id": ObjectId("5a934e000102030405000000")
},
[
{
"$set": {
"field1": "$field2"
}
}
])
mongoplayground
This question already has answers here:
$unset on multiple fields in mongodb
(2 answers)
Closed 2 years ago.
I have a collection test with a record :
[{ number:"1524",
name:null,
cat_id:"126842"},
{number:"456",
name:null,
cat_id:null }]
How to remove the whole row having null values like this :
[{ number:"1524",
cat_id:"126842" },
{ number:"456"}]
I tried this but whole records are getting deleted rather than just the rows.
collection.deleteMany( { $or: [{cat_id : null },{ name : null }]
Need to remove multiple variables in a single query.
MongoDB version 4.0
In MongoDB, the $unset operator is used to delete a particular field.
You can try using the following commands to delete field that are empty
collection.updateMany({name: null}, { $unset : { name : 1 }})
This command will delete the name field from all documents where name is null. You can do a similar command for cat_id as well.
Edit : As per an answer received by me in another question I asked, You can also do the same in a single command using the following way -
collection.updateMany({
$or : [{
name : null
}, {
cat_id : null
}]
}, [{
$set: {
name : { $ifNull : ["$hobby", "$$REMOVE"] },
cat_id : { $ifNull : ["$Roll_no", "$$REMOVE"] }
}
}]
);
However, this can only be done in MongoDb V4.2 and above. You can also look at this for another way of doing it.
Hope it helps !
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
);
This question already has answers here:
Query for a field in an object in array with Mongo?
(2 answers)
Closed 3 years ago.
So I have a collection called Transaction that is structured like the following
{
"_id": "1",
"type": "purchase",
"amount": 11.8,
"relatedObjects": [
{
"eventId": "123131313131322"
}
],
"paymentMethod" : "method1"
}
I want to query according to paymentMethod and eventId. So I was doing the following but it is not working.
db.Transaction.find({ "paymentMethod": "method1"});
and this is how I go all the transactions for "method1" but I am not sure on how should I query for eventId inside of related objects. I tried something like
db.Transaction.find({"paymentMethod": "method1", "relatedObjects[0].eventId" : "123131313131322" })
I didnt seem like it was going to work in my head but I have no clue on how to query an object inside an array.
Damn I had to read for third time the docs, sorry,
It was something like
{
"paymentMethod": "method1",
"relatedObjects" : {
$elemMatch : {
"eventId": "13213213212131321321"
}
}
}
This question already has answers here:
Update field in exact element array in MongoDB
(5 answers)
Closed 3 years ago.
Here is dummy JSON:
{
"_ID": ObjectId('xdfdsf'),
"array": [
{
"name": "Jon",
"permissions": [
"update",
"delete",
"create"
]
},
{
"name": "Ben",
"permissions":[
"update"
]
}
]
}
And so on. I want to create a query that will search the array, find element in array which has name "Ben" and pushes new permission into the array.
It's probably relatively simple to do but I got completely lost.
You can use $ positional operators to do such updates.
Try this :
db.collection_name.update({
"array.name" : "Ben"
},{
$push : {
"array.$.permissions" : "new_permission"
}
}
Read more about $(update) positional operator in official MongoDb docs for detailed information.
Sadly, Zoti's answer was incorrect, but I finally got it working:
db.myCollection.update( { "array.name": "Ben"}, {$push: {"array.$.permissions": "read"}})