Find and update value in array of nested objects in mongo [duplicate] - javascript

I have this collection:
[{ "_id" : 7,
"category" : "Festival",
"comments" : [
{
"_id" : ObjectId("4da4e7d1590295d4eb81c0c7"),
"usr" : "Mila",
"txt" : "This is a comment",
"date" : "4/12/11"
}
]
}]
All I want is to push insert a new field inside comments like this:
[{ "_id" : 7,
"category" : "Festival",
"comments" : [
{
"_id" : ObjectId("4da4e7d1590295d4eb81c0c7"),
"usr" : "Mila",
"txt" : "This is a comment",
"date" : "4/12/11",
"type": "abc" // find the parent doc with id=7 & insert this inside comments
}
]
}]
How can I insert inside the comments subdocument?

You need to use the $ positional operator
For example:
update({
_id: 7,
"comments._id": ObjectId("4da4e7d1590295d4eb81c0c7")
},{
$set: {"comments.$.type": abc}
}, false, true
);
I didn't test it but i hope that it will be helpful for you.
If you want to change the structure of document you need to use
db.collection.update( criteria,
objNew, upsert, multi )
Arguments:
criteria - query which selects the record to update;
objNew - updated object or $ operators (e.g., $inc) which manipulate the object
upsert - if this should be an "upsert"; that is, if the record does not exist, nsert it
multi - if all documents matching criteria should be updated
and insert new objNew with new structure. check this for more details

The $ positional operator is only going to work as expected if the 'comments' field is NOT an array. The OP's json is malformed, but it looks like it could be an array.
The issue is that mongodb right now will only update the first element of an array which matches the query. Though there is an RFE open to add support for updating all matching array elements: https://jira.mongodb.org/browse/SERVER-1243
To work around this issue with arrays you just have to do a regular find then update the elements in the array individually.

Related

reduce mongo db query to pure array

when i run db.abhishek.em.find({}) I have
{ "_id" : ObjectId("5ac62d35b075e574b3e7eeaa"), "name" : "first", "employed" : true }
{ "_id" : ObjectId("5ac62d3fb075e574b3e7eeab"), "name" : "second", "employed" : true }
{ "_id" : ObjectId("5ac62d4eb075e574b3e7eeac"), "name" : "third", "employed" : false }
I want to reduce this result into an array of simple object ids like this by adding or chaining something to find function , something like
db.a.b.find({employed:true}).somefunction() which can return the below array
I want to use the command nested with $in in a bigger query to achieve some sort of relational querying
[
ObjectId("5ac62d35b075e574b3e7eeaa"),
ObjectId("5ac62d3fb075e574b3e7eeab")
]
-----------------EDIT----------------------
For an example case I want to get employed employees by running
db.abhishek.another.find({id:{$in:db.abhishek.em.find({employed:true},{_id:1}).toArray()}})
or something similiar as this command is not working
The db.a.another collection is
{ "_id" : ObjectId("5ac63de1b075e574b3e7eead"), "id" : ObjectId("5ac62d35b075e574b3e7eeaa"), "name" : "Lets say person 1" }
{ "_id" : ObjectId("5ac63df7b075e574b3e7eeae"), "id" : ObjectId("5ac62d3fb075e574b3e7eeab"), "name" : "Lets say person 2" }
{ "_id" : ObjectId("5ac63e06b075e574b3e7eeaf"), "id" : ObjectId("5ac62d4eb075e574b3e7eeac"), "name" : "Lets say person 3" }
-----------------EDIT----------------------
Solved see my answer below
Use MongoDB to only provide those fields from the model. In MongoDB terms this is called Projection. There is a MongoDB method .project() that you can chain onto your query like this:
db.abhishek.em.find({}).project({});
that should do it. If you want to explicit exclude fields, you do:
db.abhishek.em.find({}).project({name:0, employed:0});
then, finally, to get the output in array form do:
db.abhishek.em.find({}).project({name:0, employed:0}).toArray();
Reference here:
https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/
Thanks everyone for providing directions and suggestions to move forward,
I was able to successfully do some sort of relational based query using the following
db.abhishek.another.find({id:{$in:db.abhishek.em.find({employed:true},{_id:1}).map(function(e) {return e._id})}},{name:1,_id:0})
It gave this as output
{ "name" : "Lets say person 1" }
{ "name" : "Lets say person 2" }
This query took all records from em collection with employed set to true,form its array and then pass it to $in operator on query for "another" collection to give output
toArray method used to convert objects nested in array hence failing $in operator
Thanks #veeram for telling about selection of fields

MongoDB aggregation: How to extract the field in the results

all!
I'm new to MongoDB aggregation, after aggregating, I finally get the result:
"result" : [
{
"_id" : "531d84734031c76f06b853f0"
},
{
"_id" : "5316739f4031c76f06b85399"
},
{
"_id" : "53171a7f4031c76f06b853e5"
},
{
"_id" : "531687024031c76f06b853db"
},
{
"_id" : "5321135cf5fcb31a051e911a"
},
{
"_id" : "5315b2564031c76f06b8538f"
}
],
"ok" : 1
The data is just what I'm looking for, but I just want to make it one step further, I hope my data will be displayed like this:
"result" : [
"531d84734031c76f06b853f0",
"5316739f4031c76f06b85399",
"53171a7f4031c76f06b853e5",
"531687024031c76f06b853db",
"5321135cf5fcb31a051e911a",
"5315b2564031c76f06b8538f"
],
"ok" : 1
Yes, I just want to get all the unique id in a plain string array, is there anything I could do? Any help would be appreciated!
All MongoDB queries produce "key/value" pairs in the result document. All MongoDB content is basically a BSON document in this form, which is just "translated" back to native code form by the driver to the language it is implemented in.
So the aggregation framework alone is never going to produce a bare array of just the values as you want. But you can always just transform the array of results, as after all it is only an array
var result = db.collection.aggregate(pipeline);
var response = result.result.map(function(x) { return x._id } );
Also note that the default behavior in the shell and a preferred option is that the aggregation result is actually returned as a cursor from MongoDB 2.6 and onwards. Since this is in list form rather than as a distinct document you would process differently:
var response = db.collection.aggregate(pipeline).map(function(x) {
return x._id;
})

MongoDB and Mongoose: How to retrieve nested Object

I have the following documents in my mongodb collection:
{
"current" :
{
"aksd" : "5555",
"BullevardBoh" : "123"
},
"history" :
{ "1" : {
"deleted" : false,
"added" : false,
"date" : "21-08-2014"
}
},
{ "2" : {
"deleted" : false,
"added" : false,
"date" : "01-01-2013"
}
},
"_id" : ObjectId("53f74dad2cbfdc136a07bf16"),
"__v" : 0
}
I have multiple of these documents. Now I want to achieve two things with my Mongoose/Express API.
Query for all nested "current" in each document and retrieve them as JSON objects like such: {"aksd":"5555","BullevardBoh":"123"},{..},{..}.
Retrieve all history revisions (1,2...) where "date" is smaller than a given date.
As you can clearly see this is a kind of versioning I am trying to implement. I would also be interested if this kind of data structure will get indexed by MongoDB and if there is possibly a better way. (e.g. with arrays inside objects?)
This isn't working in MongoDB:
db.ips.findOne({current.aksd: {$exists:true}});
I think the quotes around the field are missing here:
db.ips.findOne({current.aksd: {$exists:true}});
This should work instead:
db.ips.findOne({"current.aksd": {$exists:true}});
While Ritesh's reply was a step in the right direction, I rather wanted to fetch the current object literal and its members inside the document not the whole document.
1.) Query for all nested "current" in each document
db.ips.find({"current":{$exists:true}}, {"current":1});
This is giving back all nested documents where the aksd literal is present:
{ "current" : { "aksd" : "5555", "BullevardBoh" : "123" }, "_id" : ObjectId("53f74dad2cb3dc136a07bf16") }
...
2.) Retrieving history revisions where date is smaller then a given date:
db.ips.find({"history.date": {$lt: "01-01-2014"}},{history:{$elemMatch:{date: {$lt:"01-01-2014"}}}});
Giving back the wanted nested date literal(s):
{ "historie" : [ { "date" : "01-01-2013", "added" : false, "deleted" : false } ], "_id" : ObjectId("53faf20f399a954b2b7736b6") }

copyValue from another collection in MongoDb

Can i Copy Some Field from collection to another collection?
I want copy values of bar to foo collection, but i don't want type filed, and I want insert in foo e new _id e extra field (userId) ( then i use Node.js)
collection bar
{
"_id" : ObjectId("77777777ffffff9999999999"),
"type" : 0,
"name" : "Default",
"index" : 1,
"layout" : "1",
}
collection foo
{
"_id" : NEW OBJECT ID,
// "type" : 0, NO IN THIS COLLECTION
"userId" : ObjectId("77777777ffffff9999999911"),
"name" : "Default",
"index" : 1,
"layout" : "1",
}
I try with db.bar.copyTo("foo"); but copy entire collection
Actually that is probably your best option. But when you don't want the new field in your collection, then just remove it using $unset:
db.foo.update({ },{ "$unset": { "type": 1 } },false,true)
That will remove the field from all documents in your new collection in one statement.
In future releases from 2.6 and upwards you can also do this using aggregate:
db.bar.aggregate([
{ "$project": {
"userId" : 1
"name" : 1
"index" : 1
"layout" : 1
}},
{ "$out": "foo" }
])
The new $out method sends the output of the aggregation statement to a collection.
You can copy fields from one collection to another by using
db.copyFromCollection.find().forEach(function(x){
db.copyToCOllection.update(
{"_id" : ObjectId("53205a4a14952bee39f3376e")}, //some condition for update
{ $set:{parameterOfCopyToCollection:x.parameterOfCopyFromCollection}
});
});
Here we iterate through the data by using the above function.We iterate through the collection from which we want to copy the data and then inside the function we update the document to which we want to add/update that data.

how to push a dictionary to a nested array with mongodb?

i have data that looks like this in my database
> db.whocs_up.find()
{ "_id" : ObjectId("52ce212cb17120063b9e3869"), "project" : "asnclkdacd", "users" : [ ] }
and i tried to add to the 'users' array like thus:
> db.whocs_up.update({'users.user': 'usex', 'project' : 'asnclkdacd' },{ '$addToSet': { 'users': {'user':'userx', 'lastactivity' :2387843543}}},true)
but i get the following error:
Cannot apply $addToSet modifier to non-array
same thing happens with push operator, what im i doing wrong?
im on 2.4.8
i tried to follow this example from here:
MongoDB - Update objects in a document's array (nested updating)
db.bar.update( {user_id : 123456, "items.item_name" : {$ne : "my_item_two" }} ,
{$addToSet : {"items" : {'item_name' : "my_item_two" , 'price' : 1 }} } ,
false ,
true)
the python tag is because i was working with python when i ran into this, but it does nto work on the mongo shell as you can see
EDIT ============================== GOT IT TO WORK
apparently if i modify the update from
db.whocs_up.update({'users.user': 'usex', 'project' : 'asnclkdacd' },{ '$addToSet': { 'users': {'user':'userx', 'lastactivity' :2387843543}}},true)
to this:
db.whocs_up.update({'project' : 'asnclkdacd' },{ '$addToSet': { 'users': {'user':'userx', 'lastactivity' :2387843543}}},true)
it works, but can anyone explain why the two do not achieve the same thing, in my understanding they should have referenced the same document and hence done the same thing,
What does the addition of 'users.user': 'userx' change in the update? does it refer to some inner document in the array rather than the document as a whole?
This is a known bug in MongoDB (SERVER-3946). Currently, an update with $push/$addToSet with a query on the same field does not work as expected.
In the general case, there are a couple of workarounds:
Restructure your update operation to not have to query on a field that is also to be updated using $push/$addToSet (as you have done above).
Use the $all operator in the query, supplying a single-value array containing the lookup value. e.g. instead of this:
db.foo.update({ x : "a" }, { $addToSet : { x : "b" } }, true)
do this:
db.foo.update({ x : { $all : ["a"] } }, { $addToSet : { x : "b" } } , true)
In your specific case, I think you need to re-evaluate the operation you're trying to do. The update operation you have above will add a new array entry for each unique (user, lastactivity) pair, which is probably not what you want. I assume you want a unique entry for each user.
Consider changing your schema so that you have one document per user:
{
_id : "userx",
project : "myproj",
lastactivity : 123,
...
}
The update operation then becomes something like:
db.users.update({ _id : "userx" }, { $set : { lastactivity : 456 } })
All users in a given project may still be looked up efficiently by adding a secondary index on project.
This schema also avoids the unbounded document growth of the above schema, which is better for performance.

Categories