存储单个“一流”文件的惯用方式MongoDB 中的列表?
我有一个特殊的列表(一种队列,如数据结构中的队列,而不是工作队列中的队列),我想将其存储在 MongoDB 中。我需要经常在我的应用程序中访问和操作这个单个列表 - 而且我没有多个相同类型的列表。
将其存储在单个文档中是最简单的,但我遇到的问题是找出从我的应用程序代码引用该特定文档的最佳方法。我不想查询多个文档才能找到正确的文档,因为只有一个文档包含此列表。
我也不想将列表拆分为集合中的多个文档,因为它只是一个简短的列表(限制为 400 个元素,并且每个元素只是一小段文本,因此不存在被删除的风险)超出 4MB 文档限制)。
我想过几种可以做到这一点的方法,但似乎都不理想。请让我知道一种方法是否正确,或者我是否遗漏了某些内容。
- 为文档设置自定义
_id
,并将其硬编码到我的应用程序中。 - 为该文档创建一个集合,该集合仅包含该一个文档,以便我可以通过获取该集合中的第一个文档来检索它。 (这看起来很老套)
- 使用像
document_name: 'my_special_list'
这样的字段,并通过查询document_name
来检索文档。我不想这样做,因为我不需要像这样的其他文档,因此只有一个文档具有该document_name
字段。 - 使用文档集合,每个文档包含一个索引字段和一个元素内容字段,并使用 map-reduce 从中获取列表或类似的内容。我不想这样做,因为简单地检索 BSON 列表、操作它并将其存储回文档中要简单得多。
- 为此使用不同的技术,因为 MongoDB 不适合它。
I have a special list (a sort of queue, as in the data structure, not as in a work queue) that I want to store in MongoDB. I need to access and manipulate this single list often in my application - and I don't have several of the same type of list.
It would be easiest to store it in a single document, but the problem I'm having is figuring out the best way to reference that particular document from my application code. I don't want to have to query several documents to locate the correct one, since only one document will contain this list.
I'd also rather not split the list into several documents in a collection, since it is just a short, simple list (it is limited to 400 elements, and each element is just a short piece of text, so there is no risk of overrunning the 4MB document limit).
I've thought of several ways I could do this, but none seem ideal. Please let me know if one is the right approach, or if I'm missing something.
- Set a custom
_id
for the document, and hard-code it in my application. - Create a collection for the document which will contain only that one document, so that I can retrieve it by just getting the first document in that collection. (This seems hacky)
- Use a field like
document_name: 'my_special_list'
and retrieve the document by querying ondocument_name
. I'd rather not do this since I have no need for other documents like this, so only one document would have thatdocument_name
field. - Use a collection of documents, each containing an index field and an element content field, and use map-reduce to get a list from it, or something along those lines. I'd rather not do this since simply retrieving a BSON list, manipulating it and storing it back in the document is so much more straightforward.
- Use a different technology for this, since MongoDB isn't suited for it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MongoDB 文档 指出:
所以我会选择选项 1。
正如您自己所说,更新
选项 2 有点像 hack。
选项 3 引入了一个附加字段,您也可以使用
_id
字段。选项 4 是对简单问题的复杂解决方案。
The MongoDB documentation states:
So option 1 would be the way I'd do it.
Update
Option 2 is somewhat of a hack, as you said yourself.
Option 3 introduces an additional field for something you can use the
_id
field for just as well.Option 4 is a complex solution to a simple problem.