ChanneW
V2EX  ›  问与答

mongodb 的根据 标签 查文章怎么做?

  •  
  •   ChanneW · Aug 6, 2014 · 3270 views
    This topic created in 4482 days ago, the information mentioned may be changed or developed.
    应该说 Nosql 的对这类型问题都怎么做?
    redis 做 blog 怎么做?
    6 replies  •  2014-08-06 23:03:38 +08:00
    ijse
    ijse
    Aug 6, 2014
    1
    用MongoDB, 我是这样查的:
    ```
    find({ 'tags': { '$elemMatch': { $in [q_reg] } } })
    ```
    ChiChou
    ChiChou
    Aug 6, 2014
    2
    标签用 array 存储,如
    {
    _id: 123,
    title: "Advanced jQuery tricks",
    tag: ["frontend", "javascript", "jquery"],
    ...
    }

    查询的时候用

    db.colleciton.find({
    "title": {
    '$elemMatch': {
    'tag': {
    '$in': ['jquery', 'javascript']
    }
    }
    }
    })

    第二个问题参考《Redis 入门指南》
    http://book.douban.com/subject/24522045/
    ChiChou
    ChiChou
    Aug 6, 2014❤️1
    3
    靠,亏我辛辛苦苦手打了这么多缩进,全吃了
    ChanneW
    ChanneW
    OP
    Aug 6, 2014
    4
    @ChiChou 辛苦
    gihnius
    gihnius
    Aug 6, 2014
    5
    @ijse
    @ChiChou elemMatch 不是更多的用于数组元素是嵌套文档时的复杂查询么?
    这种情况 'tags': [x, y, z, a, b, c] 直接 $in 就可以了 find({tags: {$in: [x, y, z]}}) 或者 $all, 看具体情况.
    ChiChou
    ChiChou
    Aug 6, 2014
    6
    @gihnius 我写的例子是一次匹配多个 tag,单个的时候 $in 是足够了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1748 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 16:20 · PVG 00:20 · LAX 08:20 · JFK 11:20
    ♥ Do have faith in what you're doing.