从流星 (mongodb) 中的 document._id 中查找集合名称 [英] Find the collection name from document._id in meteor (mongodb)
问题描述
从meteor中处理mongodb相关事物的语法来看,似乎你总是需要知道集合的名称
到update、insert、remove或任何
到文件.
From the looks of the syntax for handling mongodb related things in meteor it seems that you always need to know the collection's name
to update, insert, remove or anything
to the document.
我想知道是否有可能从流星中文档的 _id
字段中获取 collection's name
.
What I am wondering is if it's possible to get the collection's name
from the _id
field of a document in meteor.
意思是如果您有一个 _id
等于 TNTco3bHzoSFMXKJT
的文档.现在知道了文档的_id
,你想找到document
位于哪个collection
.这是否可以通过meteor 的mongodb 或vanilla 实现mongodb?
Meaning if you have a document with the _id
equal to TNTco3bHzoSFMXKJT
. Now knowing the _id
of the document you want to find which collection
the document
is located in. Is this possible through meteor's implementation of mongodb or vanilla mongodb?
推荐答案
您可以通过对所有集合执行 findOne
来实现此目的:
You could accomplish this by doing a findOne
on all of the collections:
var collectionById = function(id) {
return _.find(_.keys(this), function(name) {
if (this[name] instanceof Meteor.Collection) {
if (this[name].findOne(id)) {
return true;
}
}
});
};
我在客户端和服务器上都对此进行了测试,在全局上下文中运行时似乎可以正常工作.
I tested this on both the client and the server and it seemed to work when run in the global context.
这篇关于从流星 (mongodb) 中的 document._id 中查找集合名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!