[英]How to store metadata describing documents in a MongoDB collection?
What is the best way to store metadata that describe a collection in MongoDB? 存储描述MongoDB中集合的元数据的最佳方法是什么?
I have a collection with an arbitrary and changing number of documents within. 我有一个集合,其中包含任意和不断变化的文档数量。 As I create new documents, I want to assign a new, temporary name (String) based on the number of documents currently in the collection, eg "Doc 1", "Doc 2", ... Since documents could be removed, just doing a count() will not yield a useful result. 当我创建新文档时,我想根据当前集合中的文档数量分配一个新的临时名称(String),例如“Doc 1”,“Doc 2”,...由于文档可以被删除,执行count()不会产生有用的结果。
One option is to store metadata describing a collection within a different collection, but this seems awkward. 一种选择是在不同的集合中存储描述集合的元数据,但这看起来很尴尬。
Since Mongo is schema-free, I could keep the collection's metadata within the collection itself, perhaps within a document with the name "metadata". 由于Mongo是无模式的,我可以将集合的元数据保存在集合本身中,可能在名为“metadata”的文档中。 In that case, how would I access that document quickly? 在这种情况下,我如何快速访问该文档? Would I need to build an index for "metadata" to get at it immediately, without O(n) search time? 我是否需要为“元数据”构建索引以立即获取,而无需O(n)搜索时间?
Is there a more standard, or simpler, way than what I described in the previous paragraph? 是否有比我在前一段中描述的更标准或更简单的方式? I may end up with more metadata in the future than just a count. 我可能会在未来获得更多的元数据,而不仅仅是计数。
You can check this link https://docs.mongodb.com/v3.0/tutorial/create-an-auto-incrementing-field/ . 您可以查看此链接https://docs.mongodb.com/v3.0/tutorial/create-an-auto-incrementing-field/ 。
According to the link above, my opinion is that just storing those in a colleciton named metadata
with the format: 根据上面的链接,我的意见是只将这些存储在一个名为metadata
的集合中,格式为:
{
'_id':'collection1'
'c': 0,
'other metadata1': 'foo',
'other metadata2': 'bar'
}
Then you can get those metadata according to the collection name. 然后,您可以根据集合名称获取这些元数据。 If you want to add a field to this document, just use $set with update. 如果要在此文档中添加字段,只需使用$ set with update。
What we often do is put settings or metadata or so in a separate, one-document collection .settings. 我们经常做的是将设置或元数据放在单独的单文档集合中.settings。 eg. 例如。 if your data is located in collection 'user', I put the metadata in 'user.settings' 如果您的数据位于集合'user'中,我将元数据放在'user.settings'中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.