MongoDB update elements within array - mongodb

I have a database of users like in the example below and I need to update the city of the user from Bonn to Berlin.
{
"_id" : "Louis",
"registered" : true,
"likes" : [
"tennis",
"cooking"
],
"addr" : {
"city" : "Bonn",
"country" : "Germany"
}
}
I have tried the following but it does not seem to work. Any ideas?
db.users.update( {_id:"Louis"}, {$set:{city:"Berlin"}} )

You should have quotes around field names in sub-documents
db.test.update( {_id : "Louis"}, {$set : {"addr.city" : "Berlin"}})

Related

MongoDB addToSet in nested array

I'm struggling to insert data inside a nested array in MongoDB.
My schema looks like this:
{
"_id" : ObjectId("5c0c55642440311ff0353846"),
"name" : "Test",
"email" : "test#gmail.com",
"username" : "test",
"password" : "$2a$10$RftzGtgM.DqIiaSvH4LqOO6RnLgQfLY3nk7UIAH4OAvvxo0ZMSaHu",
"created" : ISODate("2018-12-08T23:36:04.464Z"),
"classes" : [
{
"_id" : ObjectId("5c0c556e2440311ff0353847"),
"cName" : "1A",
"student" : [
{
"grades" : [ ],
"_id" : ObjectId("5c0c55812440311ff0353848"),
"name" : "StudentName",
"lname" : "StudenteLastName",
"gender" : "M"
}
insert }
],
"__v" : 0
}.
What I want to do is inserting a grade for the student inside "grades" array.
Expected result is:
{
"_id" : ObjectId("5c0c55642440311ff0353846"),
"name" : "Test",
"email" : "test#gmail.com",
"username" : "test",
"password" : "$2a$10$RftzGtgM.DqIiaSvH4LqOO6RnLgQfLY3nk7UIAH4OAvvxo0ZMSaHu",
"created" : ISODate("2018-12-08T23:36:04.464Z"),
"classes" : [
{
"_id" : ObjectId("5c0c556e2440311ff0353847"),
"cName" : "1A",
"student" : [
{
"grades" : [6],
"_id" : ObjectId("5c0c55812440311ff0353848"),
"name" : "StudentName",
"lname" : "StudenteLastName",
"gender" : "M"
}
]
}
],
"__v" : 0
}.
I tried some queries but none of them helped me, even searching a lot.
db.teachers.update({"_id": ObjectId("5c0c55642440311ff0353846"), "classes._id": ObjectId("5c0c556e2440311ff0353847"), "classes.student._id": ObjectId("5c0c55812440311ff0353848")},{$addToSet: {"classes.$.student.grades":6}})
Basically, I searched for the student with the first curly bracket (if I do "db.teachers.find(the three conditions) the result is correct) and then add to the grades array (of Integer) the value 6. But at this point I get errors, I think I'm making a mistake on the "adding" part.
I need also to do the same thing in Mongoose.
Any help is appreciated, thanks in advance!
Edit: I solved. I post my solution hoping it'll be useful to other:
For pushing inside a triple nested array do:
db.teachers.update({"_id":ObjectId("5c0c59985ae5981c58937e12"),"classes":{ $elemMatch : { _id : ObjectId("5c0c59a35ae5981c58937e13") }},"classes.student": { $elemMatch : { _id : ObjectId("5c0c59aa5ae5981c58937e14")} }},{$addToSet:{"classes.$.student.0.grades":3}})
https://docs.mongodb.com/manual/tutorial/query-array-of-documents/
Try using $elemMatch
"classes":{ $elemMatch : { _id : ObjectId("5c0c556e2440311ff0353847") }},
"classes.student": { $elemMatch : { _id : ObjectId("5c0c55812440311ff0353848")} }

How to fliter based on the embeded collection field in mongo db

I have a collection like the one below
{
"_id" : ObjectId("573eb77bf3465096ddb1e873"),
"id" : 1,
"name" : "siva",
"email" : "sivateja#gmail.com",
"address" : [
{ "street" : "balajinager", "district" : "nellore", "state" : "A.p" },
[ { "street" : "balajinager", "district" : "nellore", "state" : "A.p" } ]
]
}
I want to filter the records based on the city, which is inside the address array, how can we do that in mongo db?
district also fine?
{address:{$elemMatch:{district:"nellore"}}}
see https://docs.mongodb.com/manual/reference/operator/query/elemMatch/

Morphia - Query based on a subdocument

I have a mongo collection which has documents which look like below :
{
"_id" : ObjectId("9873214jkhdkfjdsf8324"),
"nm" : "test",
"sts" : 1,
"updby" : NumberLong(0),
"tags" : [
{
"name" : "women",
"rank" : 1,
"type" : 3
},
{
"name" : "men",
"rank" : 1
},
{
"name" : "clothing",
"rank" : 2,
"type" : 1
}
]
}
I want to query the collection such that I want all the documents which have "name": "women" and "type" : 3 inside the tags subdocument of each document which is returned.
I know that the mongo query should be something like this :
db.collection.find("tags":{
$all:[
{"$elemMatch":{"name":"women","type":3}},
]})
I tried using the 'hasthiselement' provided by morphia, but I am not able to form the exact query which I want.
getMongoDAORead().getDatastore().createQuery(test.class)
.field("tags").hasThisElement("name").equal("women");
This query doesn't seem to be correct. Can someone help me form the correct query?
I fixed this by doing the following:
I created a object of the Tags Class and initialized it:
Tags tag = new Tags("women", null, 3);
Query<MyClass> t = getMongoDAORead().getDatastore()
.createQuery(MyClass.class)
.field("ctags").hasThisElement(tag);

MongoDB query insert field into document from a list of Id's

I'm kind of stuck with the following problem. I have a MongoDB filled with documents, of these documents (I have a list with Id's) I need to insert a field.
I have this document:
{
"id" : 3639,
"type" : "P",
"createdate" : "2011-10-19T11:45:14+0200",
"name_creator" : "",
"latitude" : "50.887",
"longitude" : "9.14999",
"themes" : [{
"name" : "Fun",
"id" : "4"
}, {
"name" : "Kids",
"id" : "5"
}]
}
I need a query the can insert the themes field into the document, the current themes field does not have to be updates, just 1 new one. I have over 300 Id's where this has to be done.
The document should then look like this:
(all the other fields in themes should be removed, just one new one 'Outside')
{
"id" : 3639,
"type" : "P",
"createdate" : "2011-10-19T11:45:14+0200",
"name_creator" : "",
"latitude" : "50.887",
"longitude" : "9.14999",
"themes" : [{
"name" : "Outside",
"id" : "6"
}]
}
I would normally write a bit of Java code that would loop over the documents and change them, but I believe (hope) this could be done in a query.
Anyone got an idea on how I could do this?
Thanks for any help!
All you need to do is
db.collection.update(
{id : {$in : [your list of ids]}},
{$set : {
theme : [{
"name" : "Outside",
"id" : "6"
}]
}},
{multi : true}
)

Remove embedded document in a nested array of documents

My schema looks like this:
"content" : [
{
"_id" : ObjectId("4fc63de85b20fb72290000f8"),
"assets" : [
{
"path" : "temp/4f840af9565832fa14000002/4f840b1e565832fa14000007/4fc63de85b20fb72290000f7/content/I_Understanding_and_Measuring.pdf",
"_id" : ObjectId("4fc63def5b20fb722900010e")
},
{
"path" : "temp/4f840af9565832fa14000002/4f840b1e565832fa14000007/4fc63de85b20fb72290000f7/content/me.jpg",
"_id" : ObjectId("4fc63e4d5b20fb722900015d")
}
],
"content" : "",
"name" : "Downloads"
},
{
"_id" : ObjectId("4fc63dfd5b20fb722900012a"),
"assets" : [
{
"path" : "temp/4f840af9565832fa14000002/4f840b1e565832fa14000007/4fc63de85b20fb72290000f7/content/me.jpg",
"_id" : ObjectId("4fc63e055b20fb7229000147")
},
{
"path" : "temp/4f840af9565832fa14000002/4f840b1e565832fa14000007/4fc63de85b20fb72290000f7/content/thierry-henry-12-31-11-1.jpg",
"_id" : ObjectId("4fc63e525b20fb7229000164")
}
],
"content" : "",
"name" : "Bio"
}
],
I can retrieve this document with:
db.presentations.find({'content.assets._id': ObjectId('4fc63def5b20fb722900010e')})`
I've tried the following to remove an document from the assets collection with the line below, but to no avail:
db.presentations.update(
{'content.assets._id': ObjectId('4fc63def5b20fb722900010e')},
{$pull: {'content.assets': {'_id': ObjectId('4fc63def5b20fb722900010e')}}}
)
I'm trying to remove an item from the corresponding assets collection by its id. Any ideas?
You are so close! Remember that your outermost "content" is an array itself. So the following 2 character change works, use content.$.assets inside the value for $pull.
db.presentations.update(
{'content.assets._id': ObjectId('4fc63def5b20fb722900010e')},
{$pull: {'content.$.assets': {'_id': ObjectId('4fc63def5b20fb722900010e')}}}
)
Zoom ahead.