views:

28

answers:

1

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.

  1. Set a custom _id for the document, and hard-code it in my application.
  2. 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)
  3. Use a field like document_name: 'my_special_list' and retrieve the document by querying on document_name. I'd rather not do this since I have no need for other documents like this, so only one document would have that document_name field.
  4. 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.
  5. Use a different technology for this, since MongoDB isn't suited for it.
+1  A: 

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.

Niels van der Rest
I'm aware of that, but I'm not sure if it is primarily just to allow alternative ID schemes (like for mapping SQL data to mongodb and having the IDs line up), or if using it for "naming" special documents would be appropriate. I feel like I'd be making a special-case exception by using _id this way, rather than establishing a new "convention" as said in the documentation.
Wahnfrieden
@Wahnfrieden: I've updated my answer to clarify why option 1 is the best solution of all four. You're right that it's not really a convention as it's a one-off case. But you could define a `namedDocuments` collection where all documents have a user-defined `_id` by convention ;)
Niels van der Rest
Thanks for your update. I guess I'm just hoping that there's another option I'm missing.
Wahnfrieden