Automatically incremented in MongoDB to preserve a unique user id sequence

I am creating an analytical system, an API call will provide a unique user identifier, but it will not be consistent and too sparse.

I need to provide each unique user ID with an auto-increment identifier in order to tag a datapoint for analytics in bitrate / bitrate. Thus, the first user meetings will correspond to the first bitrara bit, the second user will be the second bit in bitarray, etc.

So, is there a reliable and fast way to generate incremental unique user identifiers in MongoDB?

+7
source share
5 answers

You can, but you should not http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field

Each object in mongo already has an identifier, and they are sorted in the order they are placed. What is wrong with getting a collection of custom objects, iterating over it and using this as an increased ID? Er is fully suited for card shrink work.

+5
source

As the selected answer, you can use findAndModify to generate sequential identifiers.

But I strongly disagree with the opinion that you should not do this. It all depends on the needs of your business. Having a 12-byte identifier can be very resource intensive and cause significant scalability issues in the future.

I have a detailed answer here .

+12
source

First, create a collection of counters that will track the last sequence value for all fields in the sequence.

db.createCollection("counters") 

Use the following code to insert this sequence document into the counters collection -

  db.counters.insert({_id:"tid",sequence_value:0}) 

Creating a Javascript Function:

  function getNextSequenceValue(sequenceName){ var sequenceDocument = db.counters.findAndModify({ query:{_id: sequenceName }, update: {$inc:{sequence_value:1}}, new:true }); return sequenceDocument.sequence_value; } 

Insert two documents:

  db.products.insert({ "_id":getNextSequenceValue("tid"), "product":"Samsung", "category":"mobiles" }) db.products.insert({ "_id":getNextSequenceValue("tid"), "product":"Samsung S3", "category":"mobiles" }) 

Get inserted documents:

  db.prodcuts.find() 

OUTPUT

{"_id": 1, "product": "Samsung", "category": "mobile phones"}

{"_id": 2, "product": "Samsung S3", "category": "mobile phones"}

+5
source

I know this is an old question, but I will post my answer to posterity ...

It depends on the system you are building and on specific business rules.

I am creating moderate to large scale CRM in MongoDb, C # (Backend API) and Angular (Frontend web application) and have found that ObjectId is absolutely terrible to use in Angular Routing to select specific objects. Same thing with API controller routing.

This suggestion worked great for my project.

 db.contacts.insert({ "id":db.contacts.find().Count()+1, "name":"John Doe", "emails":[ " john@doe.com ", " john.doe@business.com " ], "phone":"555111322", "status":"Active" }); 

The reason this is perfect for my case, but not all cases are that, as stated above, if you delete 3 entries from the collection, you will get collisions.

My business rules indicate that because of our internal SLAs, we will not be able to delete correspondence data or customer records longer than the potential duration of the application that I am writing, and therefore I simply mark the records with the listing โ€œStatusโ€, which is either โ€œActiveโ€ "or" Deleted. " You can delete something from the user interface and it will say โ€œContact deletedโ€, but the whole application has changed the contact status to โ€œDeletedโ€, and when the application calls the repository for the contact list, I filter and then delete the entries before inserting data to the client application.

Therefore db.collection.find (). Count () + 1 is the perfect solution for me ...

It will not work for everyone, but if you do not delete the data, it works fine.

+3
source

I suggest using the following ... This worked for me :)

  db.master.insert({ "id": db.master.find().count()+1, "ip": "123.456.789.101", "port": "19132", "api-key": "1234", "name": "TEST "+(db.master.find().count()+1) }) 

Using (db.master.find (). Count () + 1) Worked for my need.

-6
source

All Articles