node.js - Model.create retrive how many documents have been created -


i using model.create(array) in mongoose.
want provide user feedback how many documents have been created , how many of them haven't (i.e. didn't validate).
created callback

user.create(userstoimport, function(err, docs) {       console.log(err);     console.log(docs); } 

the problem if document not validate, receive validation error on single non-valid document, while cannot retrieve information inserted documents.
there way information?

i think, need .settle() method when.js module.

here example of doing using when.js mongoose 3.8.x:

when = require('when');  promises = userstoimport.map(function(user) {   return user.create(user); // returns promise }); when.settle(promises).then(function(results) {   // results array, containing following elements:   //   { state: 'fulfilled', value: <document> }   //   { state: 'rejected', value: <error> } }); 

it's possible without promises (e.g. using async module), code more complicated.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -