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

查看:21
本文介绍了Meteor - 通过 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 的选项.但是,Meteor 中的一般做法是使用字符串方法,因为客户端可以可靠地生成唯一 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.

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

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