Mongoose Serial Number Database

Your guide to old Mongoose serial numbers. Find our what year your BMX was built and sold and date mid school and new-school bikes made between the mid.

According to this mongodb article it is possible to auto increment a field and I would like the use the counters collection way.

Mongoose Serial Number Database Download

The problem with that example is that I don't have thousands of people typing the data in the database using the mongo console. Instead I am trying to use mongoose.

So my schema looks something like this:

I have created the counters collection in the same database and added a page with the _id of 'entityId'. From here I am not sure how to use mongoose to update that page and get the incrementing number.

There is no schema for counters and I would like it to stay that way because this is not really an entity used by the application. It should only be used in the schema(s) to auto increment fields.

HMRHMR
14.4k11 gold badges39 silver badges102 bronze badges

10 Answers

Here is an example how you can implement auto-increment field in Mongoose:

edtechedtech

You can use mongoose-auto-increment package as follows:

You only need to initialize the autoIncrement once.

mooraramoorara
1,4576 gold badges35 silver badges49 bronze badges

The most voted answer doesn't work. This is the fix:

The options parameters gives you the result of the update and it creates a new document if it doesn't exist.You can check here the official doc.

And if you need a sorted index check this doc

Alexis Tyler
1,1252 gold badges22 silver badges40 bronze badges
cluny85cluny85
Mongoose bike serial number lookup

I know this has already a lot of answers, but I would share my solution which is IMO short and easy understandable:

Make sure that entitySchema._id has type:Number.Mongoose version: 5.0.1.

SimonSimon

So combining multiple answers, this is what I ended up using:

counterModel.js

myModel.js

Akash AgarwalAkash Agarwal

I didn't wan to use any plugin (an extra dependencie, initializing the mongodb connection apart from the one I use in the server.js, etc...) so I did an extra module, I can use it at any schema and even, I'm considering when you remove a document from the DB.

And how to use it:

Hope it helps you.

(If this Is wrong, please, tell me. I've been having no issues with this, but, not an expert)

Alberto RubioAlberto Rubio

Other way is you can use external packages given by mongoose.(easy to understand)

PALLAMOLLA SAIPALLAMOLLA SAI

I use together @cluny85 and @edtech.But I don't complete finish this issues.

counterModel.findByIdAndUpdate({_id: 'aid'}, {$inc: { seq: 1} }, function(error,counter){But in function 'pre('save...) then response of update counter finish after save document.So I don't update counter to document.

Please check again all answer.Thank you.

Sorry. I can't add comment. Because I am newbie.

nguyen.thomnguyen.thom
TinhNQTinhNQ

The answers seem to increment the sequence even if the document already has an _id field (sort, whatever). This would be the case if you 'save' to update an existing document. No?

If I'm right, you'd want to call next() if this._id ! 0

The mongoose docs aren't super clear about this. If it is doing an update type query internally, then pre('save' may not be called.

Serial Number Database Software

CLARIFICATION

It appears the 'save' pre method is indeed called on updates.

Serial Number Search

I don't think you want to increment your sequence needlessly. It costs you a query and wastes the sequence number.

mschwartzmschwartz

Mongoose Serial Number Database By Name

Not the answer you're looking for? Browse other questions tagged javascriptmongodbmongooseauto-increment or ask your own question.