$addToSet does not guarantee a particular ordering of elements in the modified set.

677 views
Skip to first unread message

陈龙

unread,
Apr 23, 2014, 6:10:11 AM4/23/14
to mongod...@googlegroups.com
mongodb Official  $addToSet does not guarantee a particular ordering of elements in the modified set., but i test $addToSet element result is  insert order, i can not  comprehend $addToSet does not guarantee a particular ordering of elements in the modified set

Sam Millman

unread,
Apr 23, 2014, 6:17:19 AM4/23/14
to mongod...@googlegroups.com
That's sheer luck, how many times did you test, did you test with duplicates? Removing and re-adding etc etc etc


On 23 April 2014 11:10, 陈龙 <cl315...@gmail.com> wrote:
mongodb Official  $addToSet does not guarantee a particular ordering of elements in the modified set., but i test $addToSet element result is  insert order, i can not  comprehend $addToSet does not guarantee a particular ordering of elements in the modified set

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/42662b88-b2ab-4720-8f6f-c7d5fd4a7cd6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Glenn Maynard

unread,
Apr 23, 2014, 12:43:41 PM4/23/14
to mongod...@googlegroups.com
It does guarantee ordering in 2.4.8, as far as I can see, and changing this would probably break lots of code.  This definitely should be explicitly guaranteed.



For more options, visit https://groups.google.com/d/optout.



--
Glenn Maynard

Sam Millman

unread,
Apr 23, 2014, 12:54:52 PM4/23/14
to mongod...@googlegroups.com


> $addToSet does not guarantee a particular ordering of elements in the modified set.

Glenn Maynard

unread,
Apr 23, 2014, 12:59:25 PM4/23/14
to mongod...@googlegroups.com
Repeating the thing I'm replying to isn't really a reply.  Again, 2.4.8 *does* guarantee ordering, which would make the documentation wrong.  (I haven't looked at 2.6, but "addToSet" doesn't appear in the release notes and changing this would be a major breaking change, so the behavior should be the same.)



For more options, visit https://groups.google.com/d/optout.



--
Glenn Maynard

Sam Millman

unread,
Apr 23, 2014, 1:04:09 PM4/23/14
to mongod...@googlegroups.com
That line exists in the 2.4 documentation so is that wrong?


Sam Millman

unread,
Apr 23, 2014, 1:05:52 PM4/23/14
to mongod...@googlegroups.com
I thought it might be the older version sine I realised that doc is 2.4.10 but it is also in 2.2.7 doc

Glenn Maynard

unread,
Apr 23, 2014, 1:12:20 PM4/23/14
to mongod...@googlegroups.com
I'm not sure what you're asking, but it's the source (and live testing) that I'm looking at.  https://github.com/Tokutek/mongo/blob/master/src/mongo/db/ops/update_internal.cpp#L251  This code appears to only ever append to the array and never changes the order of existing elements, which means the documentation is incorrect.  I could be missing something in the code.





For more options, visit https://groups.google.com/d/optout.



--
Glenn Maynard

Stephen Steneker

unread,
Apr 23, 2014, 6:52:04 PM4/23/14
to mongod...@googlegroups.com
On Thursday, 24 April 2014 03:12:20 UTC+10, Glenn Maynard wrote:
I'm not sure what you're asking, but it's the source (and live testing) that I'm looking at.  https://github.com/Tokutek/mongo/blob/master/src/mongo/db/ops/update_internal.cpp#L251  This code appears to only ever append to the array and never changes the order of existing elements, which means the documentation is incorrect.  I could be missing something in the code.

Hi Glenn,

I believe the documentation sets expectations correctly -- a "guarantee" implies a commitment to testing and supporting a specific behaviour. While the current $addToSet implementation may add elements in a predictable fashion, there is no guarantee that this undocumented behaviour will remain consistent between releases. I would also point out that you are looking at the source for the TokuMX fork; they may choose to deviate on their internal implementations ;-).

The typical notion of a set is an unordered list of items with no duplicates: http://en.wikipedia.org/wiki/Set_(abstract_data_type).

Regards,
Stephen

陈龙

unread,
Apr 23, 2014, 9:27:57 PM4/23/14
to mongod...@googlegroups.com
For example
{'id':'1','orderItems':[1,2,3,4,5,6]}
first step: db.test.update({'id':'1'},{'orderItems':{'$addToSet':[7]}})
Second step: db.test.find({'id':'1'},{'orderItems':0}) ,result [1,2,3,4,5,6,7]
i can not comprehend $addToSet does not guarantee a particular ordering of elements in the modified set.,This sentence damage array original order? if not sequence insert Assuming that $addToSet can arryay[4] replace 7 and capacity move array value?

在 2014年4月24日星期四UTC+8上午12时43分41秒,Glenn Maynard写道:

陈龙

unread,
Apr 23, 2014, 9:29:06 PM4/23/14
to mongod...@googlegroups.com
For example
{'id':'1','orderItems':[1,2,3,4,5,6]}
first step: db.test.update({'id':'1'},{'orderItems':{'$addToSet':[7]}})
Second step: db.test.find({'id':'1'},{'orderItems':0}) ,result [1,2,3,4,5,6,7]
i can not comprehend $addToSet does not guarantee a particular ordering of elements in the modified set.,This sentence damage array original order? if not sequence insert Assuming that $addToSet can arryay[4] replace 7 and capacity move array value?

在 2014年4月24日星期四UTC+8上午1时12分20秒,Glenn Maynard写道:

陈龙

unread,
Apr 23, 2014, 10:22:04 PM4/23/14
to mongod...@googlegroups.com
Thank  you !

i think BSONElementSet is use judge element repeat and is not stored!

在 2014年4月24日星期四UTC+8上午6时52分04秒,Stephen Steneker写道:

Glenn Maynard

unread,
Apr 24, 2014, 11:02:39 AM4/24/14
to mongod...@googlegroups.com
On Wed, Apr 23, 2014 at 5:52 PM, Stephen Steneker <ste...@mongodb.com> wrote:
I believe the documentation sets expectations correctly -- a "guarantee" implies a commitment to testing and supporting a specific behaviour. While the current $addToSet implementation may add elements in a predictable fashion, there is no guarantee that this undocumented behaviour will remain consistent between releases. I would also point out that you are looking at the source for the TokuMX fork; they may choose to deviate on their internal implementations ;-).

If an implementation has intuitive behavior, as $addToSet does, then people are going to write code that assumes that behavior.  Putting disclaimers in the documentation doesn't change that.  Having an API that provides an intuitive behavior and then saying "we might remove this at any time" isn't good.

(I'm not looking at code for a fork, I'm looking at the mainline 2.4.8 source.  I linked a random fork because it was the first one I found from a Google search, and the code matched what I was looking at.)

The typical notion of a set is an unordered list of items with no duplicates: http://en.wikipedia.org/wiki/Set_(abstract_data_type).

MongoDB doesn't support a set data type, it only supports set-like operations on arrays, which are ordered and may contain duplicates.  $addToSet also doesn't affect duplicates.

-- 
Glenn Maynard

Sam Millman

unread,
Apr 24, 2014, 1:51:14 PM4/24/14
to mongod...@googlegroups.com
> Having an API that provides an intuitive behavior and then saying "we might remove this at any time" isn't good.

Huh? If it is made clear in the documentation why would you code differently irrespective of what the code says? Code can change without notice if it fits the unit tests, if this is not in the unit tests and is not intended behaviour by the documentation then it shouldn't be assumed that this behaviour is determined and static.

> I'm not looking at code for a fork,



> $addToSet also doesn't affect duplicates.

So how does it ensure uniqueness in an array? Are you sure your not thinking of $push?




--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
Reply all
Reply to author
Forward
0 new messages