The order of _id (ObjectId)

94 views
Skip to first unread message

Yang

unread,
Jul 25, 2012, 3:12:19 AM7/25/12
to mongod...@googlegroups.com
Does mongodb guarantee that the value of _id (ObjectId(...)) for each record is constantly increase and consequently the value of _id for each newly inserted record always greater than those already inserted records? If yes, is it true in a collection level or in a database level?

Sam Millman

unread,
Jul 25, 2012, 4:07:48 AM7/25/12
to mongod...@googlegroups.com
Mongodb cannot always say that the ObjectId will be auto incrementive in a $natural (insertion order) order state.

ObjectIds are collection level unique.

On 25 July 2012 08:12, Yang <shinwe...@gmail.com> wrote:
Does mongodb guarantee that the value of _id (ObjectId(...)) for each record is constantly increase and consequently the value of _id for each newly inserted record always greater than those already inserted records? If yes, is it true in a collection level or in a database level?

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Sam Millman

unread,
Jul 25, 2012, 4:14:01 AM7/25/12
to mongod...@googlegroups.com
Though OjbectId uniqueness should transist globally but I cannot give you an exact answer as to whether conflicts between intercollection _ids will exist in your system or not. It all depends upon the load of insertions and the timing of them tbh.

Adam C

unread,
Jul 25, 2012, 5:30:16 AM7/25/12
to mongod...@googlegroups.com
Guarantee is a tricky word, officially the answer here would have to be no because you can contrive scenarios where it might not happen.  There is also the question of where the ObjectID is generated, clock skew, and if it may have been updated manually (anyone can insert a valid value or update one), is your _id index unique etc.

The specification is here:


As you can see it is prefixed with a timestamp (so should increment automatically per unit time for its most significant bits), and has several fields including an increment at the end (optionally a random number).  Given how they are generated, there is a reasonable expectation of uniqueness as long as the specification is implemented in a sane way, but it is not guaranteed, nor can it be guaranteed that newly inserted fields will always be greater than previous data, but it is very likely.

Adam

Max Schireson

unread,
Jul 25, 2012, 5:34:58 AM7/25/12
to mongod...@googlegroups.com

Adding to Adam's explanation, because the timestamp is pretty coarse grained (seconds) you should expect in most normal usage cases thay they are unique but while they are approximately increasing they won't be strictly increasing.

-- Max

--
Reply all
Reply to author
Forward
0 new messages