I want to push Object in members array, if it's not there. is there way to check if Object is not in array in mongodb?
{
_id: 1111,
members:[
{user_id: 11},
{user_id: 12},
{user_id: 13}
]
}
So I want to check if:
newUser = {user_id: 14}
is not in members array, if not - push it there. Stuck with it. Thank you in advance for help.
db.collection.update( { _id: 1111 }, { $addToSet: { members: {user_id: 14} } } ). This will not do anything if the value is already present. More here docs.mongodb.com/manual/reference/operator/update/addToSet/…_id: 1111each time your run this query. And it will try to update document each time_idand update using$addToSet.newUseris in members array.