MongoDB design - tags

前端 未结 2 1238
孤独总比滥情好
孤独总比滥情好 2021-02-06 13:14

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

2条回答
  •  生来不讨喜
    2021-02-06 13:23

    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 } )
    

提交回复
热议问题