8

Im trying to do a simple update

Collection.update(id, {$set:{name:value}}); 

or even

Collection.update({'_id':id}, {$set:{name:value}}); 

But the collection won't update if the id is a traditional mongodb id. It only seems to work with meteors own implentation of unique id's. How can I remedy this. Is it possible for meteor to accept mongo's own id structure?

Tarang
  • 75,157
  • 39
  • 215
  • 276
  • Is your `id` variable a string or ObjectId? A string value like `502c7550bc3820529d81a9bd` won't match `ObjectId("502c7550bc3820529d81a9bd")`. – Stennie Aug 16 '12 at 04:23
  • Meteor doesn't know what an `ObjectId` is `ReferenceError: ObjectId is not defined` – Tarang Aug 16 '12 at 08:07
  • 3
    Hrm, appears that may be the case, though it seems odd to break a default feature that would be used to access existing MongoDB data :(. There is an open issue in the Meteor github queue: [update() broken when working with preexisting mongodb records using ObjectId()](https://github.com/meteor/meteor/issues/61), which seems the same problem you are trying to solve. Perhaps worth asking on the [meteor-talk google group](https://groups.google.com/forum/?fromgroups#!forum/meteor-talk) as well. – Stennie Aug 16 '12 at 08:18
  • Fixed since Meteor 0.5.7 in Feb 2013. http://www.meteor.com/blog/2013/02/21/meteor-057-major-scaling-update-new-ddp-version-ejson – Bret Fisher Aug 12 '13 at 16:48

2 Answers2

7

You're right: Meteor's DDP protocol doesn't support non-JSON types such as Mongo ObjectId. We know this is a problem: it's our oldest open issue and it's on our roadmap.

While there are definitely some "easy" quick fixes that would resolve this issue, we'd prefer to do this in the context of extending our protocol to handle other non-JSON types (dates, binary blobs, etc) rather than a specific short-term hack.

David Glasser
  • 1,438
  • 13
  • 21
6

It's possible to convert your ID into a mongodb object (on the server side) by using new ObjectID and then do the update. :

var ObjectID, require;

require = __meteor_bootstrap__.require;

ObjectID = require("mongodb").ObjectID;

Meteor.methods({
  SomeUpdate: function(upd) {
    var id;
    id = new ObjectID(upd['_id']);
    return SomeDB.update({
      _id: id
    }, {
      $set: {
        field: value
      }
    }, function(res) {
      return console.log(res);
    });
  }
});
Chris Nilsson
  • 488
  • 6
  • 11
  • It is also possible to put the objectid implentation into the mongodb package using `new MongoDB.BSONPure.ObjectID` while attaching a `var require = __meteor_bootstrap__.require; var MongoDB = require("mongodb");` and it would work on the client side when the update is done on the server end of things as long as the objectid is passed as a string inbetween – Tarang Dec 18 '12 at 11:32