流星:通过_id从集合中查找对象 [英] Meteor: Finding an object from a collection by _id
问题描述
我正在尝试使用_id用流星查找对象.
I am trying to find an object by _id with Meteor.
这是我尝试过的:
Meteor.publish("gifts", function(gid) {
console.log("Looking for "+ gid);
var gifts = Gifts.find({_id: gid}).fetch();
console.log("Result: " + gifts);
return gifts;
});
这是输出:
寻找f1790caa-7a10-4af5-a01c-e80bb2c2fd55结果:
Looking for f1790caa-7a10-4af5-a01c-e80bb2c2fd55 Result:
如果我删除查询:
Meteor.publish("gifts", function(gid) {
console.log("Looking for "+ gid);
var gifts = Gifts.find().fetch()[1];
console.log("Result:" + gifts._id);
return gifts;
});
对象在数组中,_id与上面的相同.
The object is in the array, and the _id is the same as above.
寻找f1790caa-7a10-4af5-a01c-e80bb2c2fd55结果: f1790caa-7a10-4af5-a01c-e80bb2c2fd55
Looking for f1790caa-7a10-4af5-a01c-e80bb2c2fd55 Result: f1790caa-7a10-4af5-a01c-e80bb2c2fd55
此外,如果我在mongo控制台中执行查找,则会找到对象:
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" }
我在做什么错了?
推荐答案
您从何处插入文档? MongoDB对字符串和objectIds的处理方式不同,并且看来Meteor中当前存在一个错误,无法正确处理objectIds.
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
这篇关于流星:通过_id从集合中查找对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!