流星-通过Mongo ObjectId从集合中查找文档
如果您直接在Mongo内部创建Mongo文档,并希望通过Meteor访问同一文档,那么完成此任务的最佳方法是什么?
If you create a Mongo document directly inside Mongo and want to access this same document via Meteor, what is the best way to accomplish this task?
尝试访问时出现不确定的结果.
I am getting undefined result when I attempt to access.
如果您从Meteor创建新文档,则该ID不会以ObjectId(")开头.
If you create a new document from Meteor it does not prefix the id with ObjectId("").
任何帮助将不胜感激.
我只想通过确切的ObjectId查找确切的文档.
I want to simply find exact document by exact ObjectId.
使用Meteor.Collection.ObjectID
:
var oid = new Meteor.Collection.ObjectID("a86ce44f9a46b99bca1be7a9");
var doc = SomeCollection.findOne(oid);
有关如何在集合中生成唯一ID的信息,请参见选项.但是,流星的一般做法是使用字符串方法,因为客户端随后可以可靠地生成唯一ID.
See the options for how unique IDs in collections are generated. However, it's general practice in Meteor to use the string approach because clients can then generate unique IDs reliably.