Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb: object id as short primary key within a collection

Tags:

mongodb

nosql

How to make better use of objectId generate by MongoDB. I am not an expert user, but so far i ended up creating seperate id for my object (userid, postid) etc because the object id is too long and makes the url ugly if use as the main ID. I keep the _id intact as it help indexing etc. I was wondering about any better strategy so that one can use mongo objectId as more url friendly and easy to remember key. I read the key was a combination of date etc, so any of the part can be used unique within a collection for this purpose.

thanks,
bsr/

like image 389
bsr Avatar asked Apr 25 '11 14:04

bsr


People also ask

How long is object ID in MongoDB?

ObjectId values are 12 bytes in length, consisting of: A 4-byte timestamp, representing the ObjectId's creation, measured in seconds since the Unix epoch. A 5-byte random value generated once per process.

How is MongoDB object ID generated?

MongoDB uses ObjectIds as the default value of _id field of each document, which is generated during the creation of any document. Object ID is treated as the primary key within any MongoDB collection. It is a unique identifier for each document or record. Syntax: ObjectId(<hexadecimal>).

Is there something like primary key in MongoDB?

A field required in every MongoDB document. The _id field must have a unique value. You can think of the _id field as the document's primary key.

What is an object ID and what is the size of it?

An ObjectID is a unique, not null integer field used to uniquely identify rows in tables in a geodatabasegeodatabaseThe geodatabase is the native data structure for ArcGIS and is the primary data format used for editing and data management. While ArcGIS works with geographic information in numerous geographic information system (GIS) file formats, it is designed to work with and leverage the capabilities of the geodatabase.https://pro.arcgis.com › overview › what-is-a-geodatabase-What is a geodatabase?—ArcGIS Pro | Documentation. ObjectIDs are limited to 32-bit values, which store a maximum value of 2,147,483,647.


1 Answers

If you have an existing ID (say from an existing data set), then it's perfectly OK to override _id with the one you have.

...keeo the _id intact as it help indexing etc

MongoDB indexes the _id field by default. If you start putting integers in the _id field, they will be indexed like everything else.

So most RDBMs provide an "auto-increment" ID. This is nice for small datasets, but really poor in terms of scalability. If you're trying to insert data to 20 servers at once, how do you keep the "auto-increment" intact?

The normal answer is that you don't. Instead, you end up using things like GUIDs for those IDs. In the case of MongoDB, the ObjectId is already provided.

I was wondering about any better strategy so that one can use mongo objectId as more url friendly and easy to remember key

So the problem here is that "easy to remember" ID doesn't really mesh with "highly scalable database". When you have a billion documents, the IDs are not really "easy to remember".

So you have to make the trade-off here. If you have a table that can get really big, I suggest using the ObjectId. If you have a table that's relatively small and doesn't get updated often, (like a "lookup" table) then you can build your own auto-increment.

The choice is really up to you.

like image 173
Gates VP Avatar answered Nov 15 '22 03:11

Gates VP