简体   繁体   English

如何在MongoDB集合中存储描述文档的元数据?

[英]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.

相关问题 如何在不使用上限集合的情况下在 MongoDB 中存储一组有序的文档 - How to store an ordered set of documents in MongoDB without using a capped collection mongodb 如何存储和管理元数据? - how mongodb store and manage the metadata? 如何在MongoDB中存储数据(文档) - How store data (documents) in MongoDB 如何在一个集合中找到另一个集合中的文档未引用的 MongoDB 文档? - How to find MongoDB documents in one collection not referenced by documents in another collection? 如何告诉 Spring Data MongoDB 存储文档的嵌套字段,这些字段也是它们自己的集合中的文档? - How to tell Spring Data MongoDB to store nested fields of a document that are also documents in their own collection? 如何在java中删除mongodb集合中的所有文档 - How to delete all documents in mongodb collection in java 如何计算Phalcon MongoDB集合中的文档总数 - How to count the total documents in phalcon mongodb collection 如何访问MongoDB中某个集合下的文档? - How to access documents under a collection in MongoDB? 如何检查 MongoDB 集合中是否不存在文档? - How to check if no documents exist in a MongoDB collection? 如何在MongoDB中返回没有objectId的集合的文档? - How to return documents of a collection without objectId in MongoDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM