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
Related
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 answers here:
Select Max() with "group by" in mongodb
(4 answers)
Max and group by in Mongodb
(1 answer)
Closed 3 years ago.
I have mongodb documents like this:
{'device':'A','value':12}
{'device':'A','value':13}
{'device':'A','value':14}
{'device':'B','value':4}
{'device':'B','value':5}
I would like to find
{'device':'A','value':14}
{'device':'B','value':5}
i.e. for each device (here A and B) the documents with the highest value.
What I am doing so far is to find all the distinct devices in one query and then get all the documents for each device individually sorted descending with a limit of 1. But this needs lots of queries.
Is there a way to do this in one query?
You need to use $group aggregation operator with $max accumulator.
db.collection.aggregate([
{ "$group": {
"_id": "$device",
"value": {
"$max": "$value"
}
}},
{ "$project": { "_id": 0, "value": 1, "device": "$_id" }}
])
MongoPlayground
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"}})
This question already has answers here:
MongoDB SELECT COUNT GROUP BY
(9 answers)
Closed 6 years ago.
I am using meteorhacks:aggregate package to do Mongo aggregation in Meteor. I want to get the count at the last stage of the pipeline so I use this code:
Message.aggregate([
{
$match: {
// ...
}
}, {
$count: 'count'
}
]);
It is pretty simple and should work, but I only get this error:
Exception while invoking method 'methodname'
MongoError: Unrecognized pipeline stage name: '$count'
...
Please help, thanks.
Updated: this question is not duplicated as an editor suggested, my main intention is to find out why I can not use $count
$count is available in mongodb version 3.4. For previous versions,
you will need to use $group over a constant field.
Message.aggregate([
{
$match: {
// ...
}
}, {
$group: {
_id : null,
count : {$sum : 1}
}
}
]);
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