Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor: Finding an object from a collection by _id

Tags:

mongodb

meteor

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?

like image 889
Joseph Tura Avatar asked Sep 13 '12 16:09

Joseph Tura


People also ask

What is Meteor collection?

Collections are Meteor's way of storing persistent data. The special thing about collections in Meteor is that they can be accessed from both the server and the client, making it easy to write view logic without having to write a lot of server code.

What is Meteor in Mongodb?

Meteor stores data in collections. To get started, declare a collection with new Mongo.

How do I link my meteor to Mongodb?

Open a terminal window and run meteor command. It will start running on localhost:3000 if you have not changed to port. Go to Robomongo (or your favorite mongodb client software) and create a new connection, making sure to change the connection address to localhost and the given the port number.


1 Answers

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

like image 199
shelman Avatar answered Sep 29 '22 07:09

shelman