0

I have a collection and want to update or create new document and $inc counter.

I have tried to move the update / upsert objects around, but to no effect.

Mongo Version is 3.0.11.

update = {
  query: {
    _id: "ABCDEFG1234"
  },
  update: {
    $setOnInsert: {
      $inc: {
        counter: 1
      }
    }
  },
  new: true,
  upsert: true
}

DB.collection('stats').findAndModify(update,function(e,d) { if (e) { console.log(e);} })

/*
{ MongoError: need remove or update
    at Function.MongoError.create (~/mongodb/node_modules/mongodb-core/lib/error.js:31:11)
    at commandCallback (~/mongodb/node_modules/mongodb-core/lib/topologies/server.js:1154:66)
    at Callbacks.emit (~/mongodb/node_modules/mongodb-core/lib/topologies/server.js:119:3)
    at .messageHandler (~/mongodb/node_modules/mongodb-core/lib/topologies/server.js:295:23)
    at Socket.<anonymous> (~/mongodb/node_modules/mongodb-core/lib/connection/connection.js:285:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at Socket.Readable.push (_stream_readable.js:130:10)
    at TCP.onread (net.js:542:20)
  name: 'MongoError',
  message: 'need remove or update',
  ok: 0,
  errmsg: 'need remove or update' }
*/

UPDATE

I tried

var find = { member_id: "ABCDEFG1234" };
var update = { $set: { update: Date.now() }, $inc: { web_visit: 1 } };
var options = { new: true, upsert: true, };
DB.collection('stats').findAndModify(find,update,options,fun‌​ction(e,d) { 
    if (e) { console.log(e);}
});

But it now silently fails with nothing in the 'stats' collection.

chridam
  • 100,957
  • 23
  • 236
  • 235
user3094755
  • 1,561
  • 16
  • 20
  • Have a look on these posts, it might be helpful http://stackoverflow.com/questions/20718792/how-to-use-findandmodify-function-in-mongo http://stackoverflow.com/questions/22671946/findandmodify-mongoerror-exception-must-specify-remove-or-update http://stackoverflow.com/questions/24325687/nodejs-and-mongodb-findandmodify-need-remove-or-update – Clement Amarnath Oct 28 '16 at 09:44
  • I did see one of those, but I think I'm doing exactly as recommended, but getting an error. – user3094755 Oct 28 '16 at 09:56
  • Post your sample document and expected output – Clement Amarnath Oct 28 '16 at 09:58
  • The target document is empty, the update shown above should result in a new document with a new _id & counter == 1. – user3094755 Oct 28 '16 at 12:26

1 Answers1

2

Your update query is wrongly composed; you are nesting operators which just throws an error. Since the operation you want is to set a counter field and increment it within the update, a single $inc operator would just suffice because if the counter field does not exist, $inc creates the field and sets the field to the specified value.

So a correct update operation using the Node.js driver's findAndModify() method signature would look like:

DB.collection("stats").findAndModify(
    { "_id": "ABCDEFG1234" }, // query object to locate the object to modify
    [["_id", "asc"]], // sort array
    { "$inc": { "counter": 1 } }, //document (object) with the fields/vals to be updated
    { "new": true, "upsert": true }, // options to perform an upsert operation.
    function(error, doc) { if (doc) { console.log(doc); } }
);
chridam
  • 100,957
  • 23
  • 236
  • 235
  • (I tried to make this comment in the Question, but SO would not allow it....I tried var find = { member_id: "ABCDEFG1234" } var update = { $set: { update: Date.now() }, $inc: { web_visit: 1 } } var options = { new: true, upsert: true, } DB.collection('stats').findAndModify(find,update,options,function(e,d) { But it now silently fails with nothing in the 'stats' collection. – user3094755 Oct 28 '16 at 12:46
  • You forgot to include the sort array in your method arguments: `DB.collection('stats').findAndModify(find, [["_id", "asc"]], update, options, fun‌​ction(e, d) { ... } ` – chridam Oct 28 '16 at 12:50
  • Why would I need to sort array? – user3094755 Oct 28 '16 at 12:55
  • Added Sort, still silent fail – user3094755 Oct 28 '16 at 12:58
  • Sorry...I was looking in wrong DB from shell...it is now working. Thanks. – user3094755 Oct 28 '16 at 13:00