How to implement post tags in Mongo?

前端 未结 1 1437
感情败类
感情败类 2020-12-04 16:39

I\'m toying with Mongo to make a SO-like pet project, and I want to implement post tags. Each tag has a name and a slug (string to be used as an id in the URL), and a post h

相关标签:
1条回答
  • 2020-12-04 17:41

    If the tags you use and their respective slugs are unlikely to change, I think your second approach is the better one. However I would suggest a small change - rather than storing an array of [name, slug], make the fields explicit by creating a tag subdocument as in this example post document:

    {
        "_id" : ObjectId("4ee33229d8854784468cda7e"),
        "title" : "My Post",
        "content" : "This is a post with some tags",
        "tags" : [
            {
                "name" : "meta",
                "slug" : "34589734"
            },
            {
                "name" : "post",
                "slug" : "34asd97x"
            },
        ]
    }
    

    You can then query for posts with a particular tag using dot notation like this:

    db.test.find({ "tags.name" : "meta"})
    

    Because tags is an array, mongo is clever enough to match the query against any element of the array rather than the array as a whole, and dot-notation allows you to match against a particular field.

    To query for posts not containing a specific tag, use $ne:

    db.test.find({ "tags.name" : { $ne : "fish" }})
    

    And to query for posts containing one tag but not the other, use $and:

    db.test.find({ $and : [{ "tags.name" : { $ne : "fish"}}, {"tags.name" : "meta"}]})
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题