MongoDB - update an existing document in a collection - mongodb

I have a collection called user_roles and it contains a field called rights which is an array of strings.
I want to update a document within user_roles collection that has _id = 5b1509f8b95b4bfe2b638508 by appending a new string element into the rights field.
So basically, after this update collection should hold the additional element "ui.dealers.measures.retrieve" as shown below.
{
"_id" : ObjectId("5b1509f8b95b4bfe2b638508"),
"type" : "coach",
"name" : "Coach",
"flavours" : {
"coach" : NumberInt(1)
},
"rights" : [
"ui.dealers.retrieve",
"ui.dealers.dossier.retrieve",
"ui.dealers.dossier.update",
"ui.dealers.documents.retrieve",
"ui.dealers.documents.create",
"ui.dealers.documents.delete",
"ui.dealers.events.retrieve",
"ui.dealers.events.create",
"ui.dealers.events.update",
"ui.dealers.events.export",
"ui.dealers.events.delete",
"ui.dealers.kpis.retrieve",
"ui.dealers.kpis.update",
"ui.dealers.blueprints.retrieve",
"ui.dealers.blueprints.create",
"ui.dealers.gap.retrieve",
"ui.dealers.gap.update",
"ui.dealers.measures.create",
"ui.dealers.surveys.retrieve",
"ui.dealers.surveys.update",
"ui.dealers.measures.retrieve"
],
"createdAt" : ISODate("2018-06-04T09:44:24.394+0000"),
"updatedAt" : ISODate("2018-06-04T10:01:56.428+0000")
}

Please try this
db.collection.update({_id:ObjectId("5b1509f8b95b4bfe2b638508")},{
$push:{
"rights":"ui.dealers.measures.retrieve"
}
})

Related

Get the field having same value in a mongodb collection

I have a document in my mongo db collection :
{
"_id" : ObjectId("xxxxxxx"),
"ID" : "a_11",
"details_list" : [
{
"detail" : "detail_1",
"link" : "https://xxxxx/yyy"
},
{
"detail" : "detail_2",
"link" : "https://xxxxx/zzz"
}
],
"name" : "xyz"
}
I want to get the "name" of all other docs where details_list (which is list of dictionary itself) matches with any other document in the same collection.
I tried using $where clause as well. db.collection.find({"$where": details_list == details_list}) but it did not returned any value.

mongodb already exists array added another value query

I have notification click table like this:
{
"_id" : ObjectId("5d00d4e1d720cc4cb1b24566"),
"createdOn" : ISODate("2019-06-12T10:33:05.866Z"),
"notification_center_id" : [
ObjectId("5c59343523f05e2ff13938d6")
],
"user_id" : ObjectId("5bc5dc03f6d24d29077dd362"),
"__v" : 0
}
I want to check if user_id "5bc5dc03f6d24d29077dd362" is already exists then added another notification_center_id in already exists record.
like this
{
"_id" : ObjectId("5d00d4e1d720cc4cb1b24566"),
"createdOn" : ISODate("2019-06-12T10:33:05.866Z"),
"notification_center_id" : [
ObjectId("5c59343523f05e2ff13938d6"),
ObjectId("5c5c4610c6d91403f38eda52")
],
"user_id" : ObjectId("5bc5dc03f6d24d29077dd362"),
"__v" : 0
}
I want query that first check user id exists if exists then added another notification record in notification_center_id field.
If user is not exists then I want add another record in collection
one more condition here, if notification_center_id is not inserted duplicate. if it's exist then not added.
Please help me.
db.getCollection('test').update(
{"user_id" : ObjectId("5bc5dc03f6d24d29077dd362")},
{"$push" : {"notification_center_id" : new ObjectId()}}
)
In your question, you have not mentioned what to do if user_id doesn't exist. So doing nothing in that case.
After your comment
db.getCollection('test').update(
{"user_id" : ObjectId("5c59343523f05e2ff13938d6")},
{
"$push" : {"notification_center_id" : new ObjectId()},
"$setOnInsert" : {"createdOn" : new ISODate(), "__v" : 0}
},
{"upsert" : true}
)
$addToSet update operator is used to append an element to an array and preserves uniqueness of values into an array.
db.notifications.update({"_id" : ObjectId("5d00d4e1d720cc4cb1b24566")}
, { $addToSet: { notification_center_id: new ObjectId() } })

How to view an object embedded with a document in mongodb

> db.orders.find({})
{ "_id" : ObjectId("5b78b933d62e262ddb055509"), "user_id" : "5b16d96a74be42566844e0b4", "game_id" : "5b11c56c6c71dc44976fba55", "seats" : { "_id" : ObjectId("5b78b933d62e262ddb05550a") }, "__v" : 0 }
{ "_id" : ObjectId("5bb135638625d21c0883fe1d"), "user_id" : "5b16d96a74be42566844e0b4", "game_id" : "5b11c56c6c71dc44976fba61", "seats" : { "_id" : ObjectId("5bb135638625d21c0883fe1e") }, "__v" : 0 }
The above is the output of find command on the orders stored in my Mongo Instance.
seats is an array of objects embedded in the orders schema. How can I view and fetch the array stored in Object seats?
seats is a cross-reference field which contains the ObjectIds to anther collection.
Are you using Mongoose? If so you need to use the populate method to fill in the objects on find etc.

$push removes all other fields in the document

I try to update a field in a collection that contains an array with $push. Instead of adding the value, the document gets replaced by a document containing only _id. If I do the update in the mongo-console it works.
Here's the code: (Serverside)
Meteor.methods({
dismiss: function (jobId,userId) {
Jobs.update(jobId, {$push : {dismissed: userId}},{validate: false},function(){});
},
});
The document in mongodb before looks like this:
{ "_id" : "MBfQ39BsKwqspLLEL", "title" : "Derniset edolar ingreadcomi permaap manthe soi ti.", "description" : "Siter tersion ingtery enlaringvi disicgen yma.", "wage" : 10, "wagePeriod" : "hour", "currency" : "Euro", "image" : "xGDcPNXjn7kpa2mv2", "createdAt" : ISODate("2015-06-18T06:40:47.575Z") }
And after the operation it looks like this:
{ "_id" : "MBfQ39BsKwqspLLEL" }
Any help appreciated.
Thanks.

mongodb get elements which was inserted after some document

I have a document and I need to query mongodb database to return me all the documents which was inserted after current document.
Is it possible and how to do that query?
If you do not override the default _id field you can use that objectID (see the mongodb docs) to make a comparison by time. For instance, the following query will find all the documents that are inserted after curDoc has been inserted (assuming none overwrite the _id field):
>db.test.find({ _id : {$gt : curDoc._id}})
Note that these timestamps are not super granular, if you would like a finer grained view of the time that documents are inserted I encourage you to add your own timestamp field to the documents you are inserting and use that field to make such queries.
If you are using Insert time stamp as on of the parameter, you can query like below
> db.foo.find()
{ "_id" : ObjectId("514bf8bbbe11e483111af213"), "Name" : "abc", "Insert_time" : ISODate("2013-03-22T06:22:51.422Z") }
{ "_id" : ObjectId("514bf8c5be11e483111af214"), "Name" : "xyz", "Insert_time" : ISODate("2013-03-22T06:23:01.310Z") }
{ "_id" : ObjectId("514bf8cebe11e483111af215"), "Name" : "pqr", "Insert_time" : ISODate("2013-03-22T06:23:10.006Z") }
{ "_id" : ObjectId("514bf8eabe11e483111af216"), "Name" : "ijk", "Insert_time" : ISODate("2013-03-22T06:23:38.410Z") }
>
Here my Insert_time corresponds to the document inserted time, and following query will give you the documents after a particular Insert_time,
> db.foo.find({Insert_time:{$gt:ISODate("2013-03-22T06:22:51.422Z")}})
{ "_id" : ObjectId("514bf8c5be11e483111af214"), "Name" : "xyz", "Insert_time" : ISODate("2013-03-22T06:23:01.310Z") }
{ "_id" : ObjectId("514bf8cebe11e483111af215"), "Name" : "pqr", "Insert_time" : ISODate("2013-03-22T06:23:10.006Z") }
{ "_id" : ObjectId("514bf8eabe11e483111af216"), "Name" : "ijk", "Insert_time" : ISODate("2013-03-22T06:23:38.410Z") }
>