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"
}
}
}
Related
This question already has answers here:
mongo in() clause sort by most matches
(2 answers)
Closed 9 months ago.
I've been stuck with this for a quite while.
I want to filter specials.name field based on two inputs from my form - select and input field. The problem is, whatever I tried, it always chooses on of it.
Is there any way to put if, else block inside $or to make it work?
This is the last thing that I tried, and now it works always on searchInput, which is input field, it does not react on changes in select menu.
{
$match: {
$or: [
{ 'specials.name':{$in :[searchSpecials, searchInput]}},
]
}
}
const searchSpecials = req.body.searchSpecials
const searchInput = req.body.search
If the two inputs are strings, this works as expected:
db.collection.aggregate([
{
$match: {"specials.name": {$in: ["Malinda", "Joseph"]}}
}
])
and returns:
[
{
"_id": ObjectId("5a934e000102030405000000"),
"specials": {
"name": "Malinda"
}
},
{
"_id": ObjectId("5a934e000102030405000002"),
"specials": {
"name": "Joseph"
}
}
]
As you can see here, on the playground.
No need for the $or, since the $in will return all documents that have specials.name which is included in the list of options.
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:
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:
How to remove a field completely from a MongoDB document?
(18 answers)
Closed 4 years ago.
I have a small mongo document which looks like this:
{
"_id": {
"$oid": "5aa441e898cc0b32a819be0c"
},
"details": {
"Artist": "Cyndi Lauper",
"Album": "She's So Unusual",
"ReleaseYear": 1983
},
"SongID": 1,
"SongTitle": "Girls Just Want To Have Fund"
}
I need to pull the Artist element out of the details array but keep the data in the overall document. The resulting document needs to look like this:
{
"_id": {
"$oid": "5aa441e898cc0b32a819be0c"
},
"details": {
"Album": "She's So Unusual",
"ReleaseYear": 1983
},
"SongID": 1,
"SongTitle": "Girls Just Want To Have Fund",
"Artist": "Cyndi Lauper"
}
I'm new to mongo so I'm not to strong in this. The collection is called songs so am thinking I need to do something like this:
db.songs.updateOne({},{$set : {"Artist":$pull{"detail.Artist"}}})
Any help and/or suggestions are greatly appreciated.
Jonathan
You can write something like this in the MongoDB shell:
var doc={doc:{key42:"value"},somekey:"value"};
doc.key42=doc.doc.key42;
delete doc.doc.key42;
After that you can write
doc
and press enter.
The output shold be:
{doc:{},somekey:"value",key42:"value"};
Then you can save the doc:
db.collection.save(doc);
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