I have an event collection that I look through to find a category for a specific event, and then I update my other collection with the $ push operator. The problem is that when two events have the same category, it will create a duplicate that I don't need.
I know about upserts, but I'm not sure if this is the best way to go about this? And I'm a little confused when it comes to how to write a message that works with the "$ push" -statement.
Here's what my update looks like right now:
self.users.update({"user_id": event['userid']}, {'$push': {'campaigns': UserCampaign}})
.. where:
UserCampaign = { "id": campaign['id'], "name": campaign['name'] }
The "UserCampaign" is filled with the same information from time to time, and since my collection is likely to be very huge, I want to make it as efficient as possible.
TL; DR; I want to update the array in the found document using "push", without risking getting duplicates.
source share