Nodejs plus express plus mongodb custom _id self-increment.

  node.js, question

viaThis tutorialI learned that it can be implemented by using the. findAndModify () method, and also through command-line operations, but how to write it in the node project?
2016.9.8 Update-
Why can’t you call the newID function to get a value when assigning a value to movie’s id? When executing, newID(“movieId “) is always equal to undefind, which seems to be executed by JS below when the database query is not returned. If you do not use the function, you can normally get the value by writing the else part directly in the callback inside of. findAndModify ().
How can newID(“movieId “) get the value if it must be written in the following way?

function newID(indexName){
 Counter.findAndModify(
 {_id: indexName },
 [],
 {$inc:{count:1}},
 {new:true},
 function (err,obj) {
 console.log(obj)
 Can get obj.value.count, how to write to return to the external function?
 bracket
 How to write a function that can be returned?
 bracket
 app.post('/admin/movie/new',function (req, res) {
 Slightly  ...
 if(id !  == 'undefined'){
 Slightly  ...
 }else{
 _movie = new Movie({
 _id:newID("movieId"),
 title:movieObj.title,
 doctor: movieObj.doctor,
 country: movieObj.country,
 year:movieObj.year,
 poster:movieObj.poster,
 language:movieObj.language,
 flash:movieObj.flash,
 summary:movieObj.summary
 })
 _movie.save(function (err, movie) {
 if(err){
 console.log(err)
 bracket
 Res.redirect('/movie/' plus movie._id)
 })
 );
 bracket
 });

I’d like to ask you first if you said that the increase must be 1, 2, 3, 4 … is that so? Because by defaultObjectIDIn fact, it is also self-increasing. The use of numerical sequences has its limitations, I am inThis problemInside explained why.
Official documents are also availableAn explanation of the problem.
Your problem is not how to use the driver itself, but that the understanding of event driver is incorrect.

function newID(indexName, callback) {
 Counter.findAndModify({
 _id: indexName
 }, [], {
 $inc: {
 count: 1
 bracket
 }, {
 new: true
 },
 function(err, obj) {
 //Error Handling
 console.log(obj)
 callback(obj.value.count);
 bracket
 bracket
 bracket
 app.post('/admin/movie/new', function(req, res) {
 //omitted  ...
 if (id !  == 'undefined') {
 //omitted  ...
 } else {
 newID("movieId", function(id) {
 
 _movie = new Movie({
 _id: id,
 title: movieObj.title,
 doctor: movieObj.doctor,
 country: movieObj.country,
 year: movieObj.year,
 poster: movieObj.poster,
 language: movieObj.language,
 flash: movieObj.flash,
 summary: movieObj.summary
 });
 _movie.save(function(err, movie) {
 if (err) {
 console.log(err)
 bracket
 Res.redirect('/movie/' plus movie._id);
 });
 });
 bracket
 });