Datastore solution for tag search

前端 未结 3 1168
不知归路
不知归路 2021-02-06 12:23

I\'ve got millions of items ordered by a precomputed score. Each item has many boolean attributes. Let says that there is about ten thousand possible attributes totally, each

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-06 13:20

    Mongodb can handle what you want, if you stored your objects like this

    { score:2131, attributes: ["attr1", "attr2", "attr3"], ... }
    

    Then the following query will match all the items that have att1 and attr2

    c = db.mycol.find({ attributes: { $all: [ "attr1", "attr2" ] } })
    

    but this won't match it

    c = db.mycol.find({ attributes: { $all: [ "attr1", "attr4" ] } })
    

    the query returns a cursor, if you want this cursor to be sorted, then just add the sort parameters to the query like so

    c = db.mycol.find({ attributes: { $all: [ "attr1", "attr2" ] }}).sort({score:1})
    

    Have a look at Advanced Queries to see what's possible.

    Appropriate indexes can be setup as follows

    db.mycol.ensureIndex({attributes:1, score:1})
    

    And you can get performance information using

    db.mycol.find({ attributes: { $all: [ "attr1" ] }}).explain()
    

    Mongo explains how many objects were scanned, how long the operation took and various other statistics.

提交回复
热议问题