Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update element in array if exists else insert new element in that array in MongoDb

Tags:

arrays

mongodb

I'm having group of array of element in MongoDB as given below :

{ 
    "_id" : 5, 
    "quizzes" : [
        {
            "wk" : 1, 
            "score" : 10
        }, 
        {
            "wk" : 2, 
            "score" : 8
        }, 
        {
            "wk" : 3, 
            "score" : 5
        }
      ], 
    "play" : [
        {
            "wk" : 2, 
            "score" : 8
        }, 
        {
            "wk" : 3, 
            "score" : 5
        }
    ]
}

I am trying insert new record in array if not present and if record present in that array then update that array record. Below is my MongoDB query.

db.push.update(
    { _id: 5 },
    { $push: { "quizzes": {"wk" : 6.0,"score" : 8.0},"play": {"wk" : 6.0,"score" : 8.0}  } }
)

Every time when i execute this query it inserts new record in array but i want if record present then update that array.

like image 949
5a01d01P Avatar asked Jan 27 '17 06:01

5a01d01P


People also ask

How do you update an element in an array in MongoDB?

To update a set of elements matching certain filters, we must use the filtered positional operator $[<identifier>] where <identifier> is a placeholder for a value that represents a single element of the array. We must then use the third parameter (options) of the updateMany method to specify a set of arrayFilters .

Does MongoDB update insert if not exists?

You can use upsert i.e. whenever you insert a value and it already exist then update would be performed. If the value does not already exist then it would get inserted.

How do I update a nested array in MongoDB?

Update Nested Arrays in Conjunction with $[]The $[<identifier>] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades.


1 Answers

Use $addToSet instead of $push.

db.push.update(
    { _id: 5 },
    { $addToSet: { "quizzes": {"wk": 6.0, "score": 8.0}, "play": {"wk": 6.0, "score": 8.0} } }
)

EDIT:

There is no simple built-in approach for conditional sub-document update in an array field, by specific property. However, a small trick can do the job by executing two commands in sequence.

For example: If we want to update the quizzes field with the object { "wk": 7.0, "score": 8.0 }, we can do it in two steps:

Step-1: $pull out sub-documents from the quizzes array where "wk": 7.0. (Nothing happens if matching sub-document not found).

db.push.update(
    { _id: 5 },
    { $pull: { "quizzes": { "wk": 7.0 } } }
)

Step-2: $addToSet the sub-document.

db.push.update(
    { _id: 5 },
    { $addToSet: { "quizzes": {"wk": 7.0, "score": 8.0} } }
)

You can combine the above two update commands using the bulk.find().update()

like image 153
Santanu Biswas Avatar answered Sep 20 '22 17:09

Santanu Biswas