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开发者_如何学Go 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.
The MongoDB documentation states:
Users are welcome to use their own conventions for creating ids; the
_id
value may be of any type so long as it is a unique.
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.
精彩评论