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?
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.
Meteor stores data in collections. To get started, declare a collection with new Mongo.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With