Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor collection update with traditional id

Tags:

mongodb

meteor

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?

like image 406
Tarang Avatar asked Aug 16 '12 01:08

Tarang


2 Answers

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.

like image 118
David Glasser Avatar answered Nov 15 '22 23:11

David Glasser


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);
    });
  }
});
like image 43
Chris Nilsson Avatar answered Nov 15 '22 23:11

Chris Nilsson