流星-通过Mongo ObjectId从集合中查找文档 [英] Meteor - Find a document from collection via Mongo ObjectId

查看:62
本文介绍了流星-通过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.

这篇关于流星-通过Mongo ObjectId从集合中查找文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆