I have a DB with a set of fields f1,f2,f3, where f3 is an array. I would like to do an update operation such as this
db.collection.update ({f1:1},{$push:{f3:{$each:[f2's value],$slice:-2}}})
I searched on the internet and was not abole to find anything. I am not sure if this is even possible. Any help would be much appreciated.
EXAMPLE:
This is my set of documents:
doc1 = { name: "User1", score: "Good", History of scores: ["Good", "Bad", "Average", "Bad"] }
doc2 = { name: "User2", score: "Bad", History of scores: ["Good", "Average", "Average", "Bad"] }
doc3 = { name: "User3", score: "Good", History of scores: ["Good", "Good", "Average", "Good"] }
Now suppose we have to insert the corresponding data:
{name : "User1", score: "Good"}
I would like the document to update user1's history of scores so that doc1 becomes as follows:
doc1 = { name: "User1", score: "Good", History of scores: ["Bad", "Average", "Bad", "Good"] }
another of the same update should change doc1 to:
doc1 = { name: "User1", score: "Good", History of scores: ["Average", "Bad", "Good", "Good"] }
I hope now my question has become clearer. Thanks.