I have a group of array elements in MongoDB, as shown 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 to insert a new record into an array, if it is missing, and if the record is present in this array, then update this array record. Below is my MongoDB request.
db.push.update( { _id: 5 }, { $push: { "quizzes": {"wk" : 6.0,"score" : 8.0},"play": {"wk" : 6.0,"score" : 8.0} } } )
Each time I execute this query, it inserts a new record into the array, but I want the record to be present, and then update this array.
source share