Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB design - tags

Tags:

.net

mongodb

norm

I'm new with MongoDB. I have a design question, about performance of MongoDB. Lets say I have the class Movies with two properties, Name and Director. Also I want to tag this Movie Class. Is better to add a new propertie of strings[] to this class, or to create a new class MovieTags? I know I will query this tags a lot because I will use an autocomplete on the UI. For this autocomplete function I only need the tags, not the Movie object. What option is better? add a propertie of strings[] or reference to a collection of MovieTags? Thinking in performance... of course in both cases the indexing will be done.

Should I use a MapReduce? To only select the tags, for the autocomplete function if I use an embebed string[] object? How?

Thanks!

like image 925
elranu Avatar asked May 20 '11 16:05

elranu


2 Answers

I'd probably go with a schema like this, which stores the tags in a string array field:

db.movies.insert({
    name: "The Godfather",
    director: "Francis Ford Coppola",
    tags: [ "mafia", "wedding", "violence" ]
})

db.movies.insert({
    name: "Pulp Fiction",
    director: "Quentin Tarantino",
    tags: [ "briefcase", "violence", "gangster" ]
})

db.movies.insert({
    name: "Inception",
    director: "Christopher Nolan",
    tags: [ "dream", "thief", "subconscious" ]
})

You wouldn't need map-reduce for this type of query. By embedding the tags inside the the movie document you can take advantage of MongoDB's multikey feature, and find movies with a given tag using single find() query like this:

db.movies.find( { tags: "dream" } )

And like you said, it's also worth adding an index to the multikey array to improve query performance:

db.movies.ensureIndex( { tags: 1 } )
like image 188
Chris Fulstow Avatar answered Sep 20 '22 15:09

Chris Fulstow


You can always filter the fields that are returned as part of the query result.

The link to the docs that details how to do so is http://docs.mongodb.org/manual/tutorial/query-documents/#Querying-FieldSelection

This will let you filter out parts of the movie object that you re not interested in.

like image 35
Shekhar Avatar answered Sep 22 '22 15:09

Shekhar