mongodb is so good
-
We can update 'array' filed with a unique value with "$addToSet". How amazing is it? I used to get the field value, check the new data is in the array or not, and save the array back.
urls = [j.url for j in a.followers] db.graph.update_one({"user_id": a.url}, {"$addToSet": {"following": {"$each": urls}}})
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.
-
Also, the index is super important in MongoDB. Don't forget you can create an index on a field that will be created.