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 8946463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:31:26+00:00 2026-06-15T12:31:26+00:00

Given: Connection is Safe=True so Update’s return will contain update information. Say I have

  • 0

Given: Connection is Safe=True so Update’s return will contain update information.

Say I have a documents that look like:

[{'a': [1]}, {'a': [2]}, {'a': [1,2]}]

And I issue:

coll.update({}, {'$addToSet': {'a':1}}, multi=True)

The result would be:

{u'connectionId': 28,
 u'err': None,
 u'n': 3,
 u'ok': 1.0,
 u'updatedExisting': True
}

Even when come documents already have that value. To avoid this I could issue a command.

coll.update({'a': {'$ne': 1}}, {'$push': {'a':1}}, multi=True)

What’s the Time Complexity Comparison for $addToSet vs. $push with a $ne check ?

  • 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-15T12:31:27+00:00Added an answer on June 15, 2026 at 12:31 pm

    Looks like $addToSet is doing the same thing as your command: $push with a $ne check. Both would be O(N)

    https://github.com/mongodb/mongo/blob/master/src/mongo/db/ops/update_internal.cpp

    if speed is really important then why not use a hash:

    instead of:

    {'$addToSet': {'a':1}}
    {'$addToSet': {'a':10}}
    

    use:

    {$set: {'a.1': 1}
    {$set: {'a.10': 1}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to increment all documents in a collection that contain a given nested
I am building a .NET application that given a connection string, at run time,
I have given client's queue connection details to connect to it using Camel. While
I am trying to do ssh connection using cygwin. I have followed instructions given
I have a method connection(int n) which gives me all the cells number that
On reading lots of code, I find that a given Connection object, which implements
For given Connection instance how do I find out url that the Connection uses
I have a Queue object that I need to ensure is thread-safe. Would it
Among FTP, HTTP, SSH, etc. and given a consistent, broadband internet connection what is
I'm aware that similar questions on SO are numerous, but I have a caveat.

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.