How to organize my documents? - mongodb

I'm pretty new to mongodb (only work with it for one small project) and I wanted to have your tips on how to organize my documents. My brain is not (yet) nosql formatted.
I have a collection storing all kind of informations and I want to add tags to it. There will be 1-5 tags by document. I want to be able to search by tags (among other things), display all the documents for 1 or more given tags, know the number of documents by tag.
What do you think is the best way to approach this simple problem ? should I give it his own collection ? should I embed it ?
How would you do ?
Thanks

Embed the tags in the document. You can search on embedded arrays and you can index them.

Related

MongoDB sorting by documents with more fields filled out

I'm currently trying to get a query that I can find by zip code (very easy)
e.g. db.col.find({zip: 60010})
but then sort based on how many fields on the Mongo document are filled out (or not null; not so easy)
How can I do this in a fast an efficient way?
You can do it using objectToArday and addFields then size
You can refer this answer
But it's costlier option than having one field which tells the count of fields in that document.
You can decide based on read or write intensive application.

MongoDB database design - contest application

I'm building a contest application. Which have 4 collections so far:
contest
questions
matches
users
I want to store every user score for every match he's assigned into. But I really can't find a proper way to achieve this.
All what I've came up with, Is to replace matches in users with an array in which each element contains a reference to matches collection and score field. But I think this is not very efficient.
EDIT
I was thinking about another solution. A separate collection called scores that contains three fields user, match and score.
Here's my schema structure:
Contests:
Questions:
Matches:
Users:
Note Any recommended adjustments on the current design is welcomed too.
Since mongodb is not designed to support collections relationships you migth end up with some duplicated work, I would suggest you to find a way of storing as much data as you can in a single document.
Your scores would go in each match document, probably the users array would have this structure {'users':[{user_id:'xxx',score:xxx}{user_id:'xxx',score:xxx}]}
The other solution, would be what you say, to have in each user doccument, a matches array with a structure like this: {'matches':[{match_id:'xxx',score:xxx}{match_id:'xxx',score:xxx}]}
You can have both also, this migth be more efficient depending the kind of queries you will need to do. You can also have a field in the subdocuments that stores the user/match name/title
Note: As you can see, you have two solutions, or you optimize for doccument size(so you can store more) or you optimize for performance (so you can read faster/with less resources)
Hope this be of any help.

How should I store the "tags" of a document in MongoDb?

I know this question was asked several times, but as far as my search skills go, every post is how to implement this in a SQL database, and no mention of NoSQL databases.
I have documents for which I want to implement a tagging feature.
Users will be able to tag them with whatever string they want, and then I need to be able to query the documents as fast as possible (by these generic tags)
Should I have a String array for my tags (which would allow me to support any number of tags), like this
{"_id":"aaa", "prop":"value", "tags":["tag1","tag2","tagN"]}
or limit the amount of tags to, say, 5, and have them as different properties, like this
{"_id":"aaa", "prop":"value", "tag1":"value", "tag2":"value", "tag3":"value" }
Which structure would be better for fast querying, specifically in mongodb?
Using the second structure would allow me to index the collection by this fields, but are 5 indexes recommended? Should I have less tags?
You simply want to use an array, the first example you have. That will allow you to have a consistent model and indexes.
{"_id":"aaa", "prop":"value", "tags":["tag1","tag2","tagN"]}
If you index a field that contains an array, MongoDB indexes each value in the array separately, in a “multikey index.”[1]
[1] http://docs.mongodb.org/manual/core/indexes/#multikey-indexes

"Pointers" in MongoDB?

In the project I am currently working on, it seems to make more sense efficiency wise if I create a nested document that contains a list of "pointers" to information stored in other collections. That way this nested document can be easily used to retrieve a list of relevant information. The question is, how to do this? Is there a way to store locations of other information in a field in MongoDB? If not, could anyone suggest a scheme that is equally or more efficient? Thanks very much!
There is no GOOD way to do this. If this is what you're looking for, you should be using a relational database.
But if you HAVE to go by this route then, why not store ID's in a document, and then link those ID's to documents in the other collection.
Unfortunately, this would require you to do 2 separate queries, as Mongo does not support compound queries that span documents.

Sphinx search engine and related tags

I'm using Sphinx search engine to index all my Intranet documents using tags. With that I don't have any trouble to find specific documents with one ore more tags.
I want to go further with a new feature like the StackOverflow "related tags" feature.
Does anybody know the best way to do this with Sphinx ?
Thanks
You run a boolean OR query on all terms in the document you want to find related items for. It can be fairly slow because all documents in the database has to be ranked on similarity, unless you limit the search using and:ed terms. See my text here: https://stackoverflow.com/questions/3121266/efficient-item-similarity-search-using-sphinx