Updating a specific item in an array using MongoDB / Meteor

"users_voted" : [ 
    {
        "user_id" : "AQG8ECLdBRJ4jwPMG",
        "score" : "down"
    }
]

I wonder how I will update the field users_voted, which is an array object. I need to update a specific object. I know indexwhere this object is located, I just need to figure out how I can update this object in the MongoDB / Meteor collection.

This is some pseudo code that I need to better explain what I mean.

Posts.update({_id: post_id}, {$set: {vote_score[index]: u_object}});

So, in this request, I know indexand post_id, and u_object- this is an object that I am trying to put in an array instead of any object that was there in index. If anyone could help, let me know how I should do it, that would be great.

+4
source share
1

. :

var obj = {};
obj["users_voted." + index] = u_object;
Posts.update({_id: post_id}, {$set: obj});
+8

Source: https://habr.com/ru/post/1532899/


All Articles