通过ObjectID获取MongoDB集合 [英] Get MongoDB collection by ObjectID
问题描述
MongoDB ID对于单个数据库集群而言是唯一的.是否可以获取使用特定ObjectID的集合?
MongoDB IDs are unique for a single db cluster. Is it possible to get the collection a specific ObjectID is used in?
我有一个ObjectID的十六进制表示,需要知道具有该ID的文档所在的集合.可能吗?
I have the hex representation of an ObjectID and need to know the collection the document with this ID is in. Possible?
推荐答案
这不是本机功能,但可以通过遍历所有集合(db.getCollectionNames())
并调用find({_id: <yourid>})
查询来实现.显然,这将是一个非常缓慢的操作,因此更改模式或使用ObjectId以外的其他内容作为_id值可能是您的前进之路.
It's is not a native feature but it is possible by iterating over all collections (db.getCollectionNames())
and invoking a find({_id: <yourid>})
query. Obviously this will be a very slow operation so a schema change or using something other than ObjectId as your _id value is probably the way forward for you.
这篇关于通过ObjectID获取MongoDB集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!