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.
HMRHMR10 Answers
Here is an example how you can implement auto-increment field in Mongoose:
You can use mongoose-auto-increment
package as follows:
You only need to initialize the autoIncrement
once.
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 TylerI 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
.
So combining multiple answers, this is what I ended up using:
counterModel.js
myModel.js
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)
Other way is you can use external packages given by mongoose.(easy to understand)
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.
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.