0

I use the following update query in mongoose:

Doc.findOneAndUpdate({
  name: 'Ekik'
}, {$setOnInsert: {key: value}}, {upsert: true, new: true}, function (err, doc) {
     // How to get upserted count here?
     console.log(doc.isNew); // always true either document upserted or don't
});

I need to get count of inserted documents. How could I do it?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Erik
  • 14,060
  • 49
  • 132
  • 218
  • You do realize you are passing in the ObjectId value as the query? Either you just generated this so **one inserted** or you got that from somewhere and expect it to exist so **one updated**. – Neil Lunn Jun 02 '14 at 06:35
  • I need to hack like the following http://stackoverflow.com/questions/16358857/mongodb-atomic-findorcreate-findone-insert-if-nonexistent-but-do-not-update but I just need to know either document was inserted – Erik Jun 02 '14 at 06:39
  • Read the comment again, it's pretty clear. You are finding by _id. So does it exist in the collection or not. the "One" in the name of the method should give you a hint to how many. – Neil Lunn Jun 02 '14 at 06:40
  • Unfortunately I don't understand how could I get upserted document. Either I use {new: true} I always get some document but I can't know either it upserted or existed – Erik Jun 02 '14 at 06:45
  • new: `true` is the default being the modified document. Try new: `false` – Neil Lunn Jun 02 '14 at 06:47
  • If I set {new: false} I don't get upserted document. I need to get only upserted document in the callback above or null if there is no upserted one – Erik Jun 02 '14 at 06:49

1 Answers1

0

The return signature of findOneAndUpdate() in the callback is the err and the document that was either modified or inserted. There is also a default case that the document returned is the "modified" document unless you set the argument to false.

Looking an 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 the document for the given _id existed then I would expect to see the "original" document without modifications returned. There is only $setOnInsert in there anyway so there would be no change.

If however an "upsert" occurred the return value for the "original" document would be null since there was no document originally. This is what tells you the operation resulted in an "upsert" and created a new document.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • Thanks for the response but why we can't know either document upserted by using isNew value. (I've updated the post) – Erik Jun 02 '14 at 07:14
  • @Erik As I told you when `new: false` then an a new document results in the the `doc` value in the callback being `null`. Otherwise it is the original document. `null` means a new document was inserted. – Neil Lunn Jun 02 '14 at 07:16
  • Yes I understand but I need to get inserted document in callback method. How could I get it – Erik Jun 02 '14 at 07:17
  • @Erik Well your only arguments are the `_id` and `$setOnInsert` so those are the only values that are in the document. Alternately leave the default `new: true` and if the `$setOnInsert` value is not reflected in the returned document then it was not inserted but only updated. It's just a case of logic to which method you choose. – Neil Lunn Jun 02 '14 at 07:22
  • Actually I have no Object for query and update document (it's just example) So are any possibilities to know what I need? – Erik Jun 02 '14 at 07:30
  • @Erik I really don't understand how I can make this any clearer than what I have already said. There are several ways presented of how to tell whether the document is inserted as a new document or not, it is a basic comparison to the arguments you supply which are either present or not. – Neil Lunn Jun 02 '14 at 07:35