Avoid a duplicate value when I update array using $push on MongoDB
The best way to do this is using $addToSet
operator which ensures that there are no duplicate items added to the set and the $each
modifier to add multiple values to the"following" array.
urls = [j.url for j in a.followers]db.graph.update_one({"user_id": a.url}, {"$addToSet": {"following": {"$each": urls}}})
also you should use the update_one
method because update
is deprecated.
I think, that you can use $addToSet operator: https://docs.mongodb.org/manual/reference/operator/update/addToSet/