mongoosejs/mquery

Long causes Client Error: bad object in message: invalid bson type

Opened this issue · 0 comments

Using the MongoDB Long/NumberLong type in queries can cause a few different errors in mquery. For example:

mquery(collection).update(
    {_id: "example"},
    {_id: "example", number: mongodb.Long.fromString("12345")},
    {upsert: true},
    function(err, num) {
      if (err) throw err;
      console.log("Updated " + num);
    }
);

Causes Client Error: bad object in message: invalid bson type while

mquery(collection).update(
    {_id: "example"},
    {number: mongodb.Long.fromString("12345")},
    function(err, num) {
      if (err) throw err;
      console.log("Updated " + num);
    }
);

Causes Client Error: bad object in message: bson length doesn't match what we found

The root of the problem appears to be that utils.clone and utils.cloneObject do not properly handle the mongodb.Long type (and likely Timestamp and possibly other non-native BSON types).

This could be fixed by adding another obj.constructor special case to clone for these types. Alternatively, the reason that the clone doesn't currently work is that functions are cloned by converting their source to a number and calling the Function constructor, which doesn't work for the functions on the Long prototype. In either case, it shouldn't be too bad to fix.

Cheers,
Kevin