Full text search in Pymongo

前端 未结 2 1880
旧时难觅i
旧时难觅i 2021-02-05 15:41

The upcoming MongoDB 2.4 supports full-text search.

We do this in the mongo shell with a command, such as

db.players.runCommand(\"text\", {
    \"search\         


        
相关标签:
2条回答
  • 2021-02-05 16:24

    Figured it out: pymongo uses keyword arguments for the additional command arguments:

    db.command("text", "players", 
        search="alice", 
        project={"name": 1, "_id": 0}, 
        limit=10)
    

    The reason for the odd error message "no such cmd: project" is that Python's dictionaries are unordered, and the project key happened to be first when passed to mongo.

    0 讨论(0)
  • 2021-02-05 16:28

    An alternative solution is to use OrderedDict. Assuming collection and query are given as variables while additional parameters such as limit, projection and others given in the dict 'params':

    params_ord = OrderedDict()
    params_ord['text'] = collection
    params_ord['search'] = query
    for k,v in params.iteritems():
        params_ord[k] = v
    db.command(params_ord)
    
    0 讨论(0)
提交回复
热议问题