Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8089999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:34:51+00:00 2026-06-05T19:34:51+00:00

I have an event collection which I’m going through to find a category to

  • 0

I have an event collection which I’m going through to find a category to the specific event and then I’m updating my other collection using a $push statement. The problem is that when two events have the same category it’ll create a duplicate, which I don’t want.

I know about upserts but I am unsure if they are the best way to go regarding this? And I’m a bit confused when it comes to how to actually write an upsert that works with the “$push”-statement.

This is how my update looks right now:

self.users.update({"user_id": event['userid']}, {'$push': {'campaigns': UserCampaign}})

..where:

UserCampaign = {
        "id": campaign['id'],
        "name": campaign['name']
}

The “UserCampaign” gets filled up with the same information from time to time, and since my collection is probably gonna be very huge I want to complete this as efficient as possible.

TLDR; I want to update the array in the document found using a “push” without having the risk of getting duplicates.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:34:54+00:00Added an answer on June 5, 2026 at 7:34 pm

    Found a better answer to my problem:

    By using $addToSet it didn’t create duplicates (I also made sure no duplicates where made before by adding all dictionaries to a list):

    self.users.update({"user_id": event['userid']}, {'$addToSet': {'campaigns': UserCampaigns[i]}})
    

    If I just had used $push it would always create duplicate elements in ‘campaigns’ within the users collection. This happened with and without upsert.

    For some reason $each didn’t work but wasn’t required, I guess PyMongo takes care of that for me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a map which adds a collection of markers using a for loop
Our application is modulraized (Group of modules doing specific things). The modules have event
I have got a collection of viewModels(InputViewModel) in an other viewModel(ScenarioManager). each InputviewModel has
I have a Toolbar whose ItemSource is a collection of toolbarItems which contain the
I have the following Backbone.js model and collection: // Model lr.Event = Backbone.Model.extend({}); //
I have a DataGridView bound to the collection, which is changed frequently (not collection
I have a Backbone collection with a load of models. Whenever a specific attribute
I have a ListBox which displays the collection of MyObjects. The collection is in
I have a class testviewModel which has implemented INotifychanged event and all its proeprties.
I have a simple program which displays a collection on a data grid. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.