node.js - How to get count of upserted documents with $setOnInsert? -


i use following update query in mongoose:

doc.findoneandupdate({   name: 'ekik' }, {$setoninsert: {key: value}}, {upsert: true, new: true}, function (err, doc) {      // how upserted count here?      console.log(doc.isnew); // true either document upserted or don't }); 

i need count of inserted documents. how it?

the return signature of findoneandupdate() in callback err , document either modified or inserted. there default case document returned "modified" document unless set argument false.

looking an example case:

    var mongoose = require('mongoose'),         schema = mongoose.schema,         objectid = require('mongodb').objectid;      mongoose.connect('mongodb://localhost/test');      var uptestschema = new schema({       value: number     });      var uptest = mongoose.model( "uptest", uptestschema, "uptest" );      var id = new objectid("538c1fea6369faeced1b7bfb");      console.log( id )      uptest.findoneandupdate(       { _id: id },       { "$setoninsert": { value: 3 } },       { upsert: true, new: false },       function(err, doc) {          console.log( doc );        }     ); 

now if document given _id existed expect see "original" document without modifications returned. there $setoninsert in there anyway there no change.

if "upsert" occurred return value "original" document null since there no document originally. tells operation resulted in "upsert" , created new document.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -