数组中不存在元素时$ addToset与$ push的时间复杂度 [英] Time Complexity of $addToset vs $push when element does not exist in the Array
问题描述
鉴于:连接安全=正确,因此Update的返回将包含更新信息.
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]}]
然后我发出:
coll.update({}, {'$addToSet': {'a':1}}, multi=True)
结果将是:
{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)
$ addToSet与带有$ ne支票的$ push的时间复杂度比较是什么?
What's the Time Complexity Comparison for $addToSet vs. $push with a $ne check ?
推荐答案
类似于 $ addToSet 的操作与您的命令相同: $ push带有$ ne的检查.两者都是O(N)
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:
代替:
{'$addToSet': {'a':1}}
{'$addToSet': {'a':10}}
使用:
{$set: {'a.1': 1}
{$set: {'a.10': 1}
这篇关于数组中不存在元素时$ addToset与$ push的时间复杂度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!