Time Complexity of $addToset vs $push when element does not exist in the Array

后端 未结 3 1593
鱼传尺愫
鱼传尺愫 2021-02-06 01:59

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]}, {         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-02-06 02:17

    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}
    

提交回复
热议问题