5

I am trying to find an object by _id with Meteor.

Here is what I have tried:

Meteor.publish("gifts", function(gid) {
  console.log("Looking for "+ gid);
  var gifts = Gifts.find({_id: gid}).fetch();
  console.log("Result: " + gifts);
  return gifts;
});

This is the output:

Looking for f1790caa-7a10-4af5-a01c-e80bb2c2fd55 Result:

If I take out the query:

Meteor.publish("gifts", function(gid) {
  console.log("Looking for "+ gid);
  var gifts = Gifts.find().fetch()[1];
  console.log("Result:" + gifts._id);
  return gifts;
});

The object is in the array, and the _id is the same as above.

Looking for f1790caa-7a10-4af5-a01c-e80bb2c2fd55 Result: f1790caa-7a10-4af5-a01c-e80bb2c2fd55

Also, if I execute the find in a mongo console, I find the object:

> db.gifts.find({_id: 'f1790caa-7a10-4af5-a01c-e80bb2c2fd55'});
{ "name" : "A new gift", "_id" : "f1790caa-7a10-4af5-a01c-e80bb2c2fd55" }

What am I doing wrong?

Joseph Tura
  • 6,290
  • 8
  • 47
  • 73

1 Answers1

5

Where did you insert the document from? MongoDB treats strings and objectIds differently, and it looks like there is currently a bug in Meteor that does not handle objectIds correctly.

https://github.com/meteor/meteor/issues/61

shelman
  • 2,689
  • 15
  • 17
  • 1
    Thank you for the pointer. Surprising that something as basic as this is not working, even given the early stage Meteor is in. – Joseph Tura Sep 14 '12 at 07:35
  • In meteor version 1.4, the behaviour can still be observed (I ran in the same pitfall). The issue linked by @shelman recommends using `meteor shell` instead of the mongo console. – zliw Oct 24 '16 at 08:21