0

I'm trying to do a simple document update using Monk however the following exception gets thrown:

.../node_modules/monk/node_modules/mongoskin/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:35
    throw new Error("Argument passed in must be a single String of 12 bytes or

My code is quite simple:

var db = require('monk')('localhost/app'),
    raw = db.get('raw'),
    stats = db.get('daily');

stats.updateById("1_20141108", {$set: { last_updated: 123 }, $inc: {"stats.facebookcom":1}}, function() {});

I've tried using update, updateById, findAndModify methods but it still throws the same error.

Inserting works fine, so I'm not sure what's going on. How do I update a record?

Ignas
  • 1,965
  • 2
  • 17
  • 44
  • This comes from "mogoskin" which monk is built on. The `_id` key is expected to be an `ObjectId`. Any "string" is therefore "autocast" this way. There is no current workaround. – Neil Lunn Nov 08 '14 at 11:52
  • Sorry, I'm not sure I follow. "1_20141108" should be autocast to ObjectId so why doesn't it? – Ignas Nov 08 '14 at 12:08
  • So do I have to change libraries to do a simple update? – Ignas Nov 08 '14 at 12:08
  • Very much as the basic [MongoDB driver library library](https://github.com/mongodb/node-mongodb-native) (for node.js) does no do this. Other abstractions such as [mongoose](http://mongoosejs.com/) allow "type" overriding by "schema" definitions. But as I have said, ["mongoskin"](https://github.com/kissjs/node-mongoskin) is pretty much "opinionated" in how things are done. So you can always request a "patch" to the author. – Neil Lunn Nov 08 '14 at 13:47
  • I've actually replaced monk with Mongoose and it works fine now. Thanks for the help :) – Ignas Nov 08 '14 at 14:08

0 Answers0