0

My application utilizes SQL Server and MongoDB. In order to link records / documents from both sides, I'm thinking to use SQL's BigInt value (Int64 in C#) in place of MongoDB's _id. This BigInt value is generated from a SQL Server Identity column. I thought it's perfect.

However, in this thread Is it bad to change _id type in MongoDB to integer? someone mentioned MongoDB has to check its uniqueness before inserting it. So is it true MongoDB doesn't check uniqueness if ObjectId.GenerateNewId() is used otherwise does?

Community
  • 1
  • 1
  • A point of clarification for interpreting the [related question](http://stackoverflow.com/questions/14054384/) you referenced. ObjectIDs are constructed using a combination of values that have a high probability of uniqueness when generated independently across a cluster of machines, but the `_id` index still ensures uniqueness when inserting new documents. The question was about whether it's a good idea to have *incrementing* numeric IDs. Generating incremental IDs puts a dependency on a central source to find the next number in the sequence (and/or retrying on duplicate key exception). – Stennie Jan 22 '15 at 13:58
  • In your case, it sounds like you already have assurance that your numeric IDs are unique because you are mirroring data that has been inserted into SQL Server. Using the same primary key in both databases makes sense if the insertions are always flowing from SQL Server to MongoDB (i.e. you don't have to worry about coordinating how the unique IDs are generated). – Stennie Jan 22 '15 at 14:03
  • So ObjectID is similar to SQL's NEWID(), type of uniqueidentifier. – myafternoontea1 Jan 23 '15 at 03:38
  • Yes, ObjectID to SQL Server's NEWID() is a fair comparison. ObjectIDs are 12-byte unique identifiers; NEWID() generates 16-byte unique identifiers. Both are designed to be independently generated. An [ObjectID](http://docs.mongodb.org/manual/reference/object-id/) does have a leading time-based prefix which can be useful for approximate ordering of generated IDs. – Stennie Jan 23 '15 at 06:12

1 Answers1

0

Mongodb does check uniques of id even if its ObjectId type.

When you create new collection it comes automatically with an unique index on _id field.

_id Field Index

MongoDB creates the _id index, which is an ascending unique index on the _id field, for all collections when the collection is created. You cannot remove the index on the _id field.

http://docs.mongodb.org/manual/core/index-single/http://docs.mongodb.org/manual/core/index-single/#cases

Exception to this rule are capped collection which does not have this index

As Stennie spotted capped collection in MongoDb2.2+ have _id field indexes

marcinn
  • 1,786
  • 11
  • 13
  • 1
    FYI, capped collections created after MongoDB 2.2 also require an indexed unique `_id` field. In particular, the `_id` is [used for replication](http://docs.mongodb.org/manual/core/capped-collections/#recommendations-and-restrictions). – Stennie Jan 22 '15 at 13:38