如何使用python在mongodb中启用textsearch?

2024-05-14 18:06:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在下面的脚本中编写了textSearchedEnabled=true,但是出现了语法错误。我不明白如何使用python在mongodb中启用文本搜索。在

 import json
 import pymongo # pip install pymongo
 from bson import json_util # Comes with pymongo
 from pymongo import MongoClient
 --setParameter textSearchEnabled=true
 client = pymongo.MongoClient('mongodb://user:user123@ds033499.mongolab.com:33499/enron')
 db = client.enron
 mbox = db.mbox

 # Create an index if it doesn't already exist
 mbox.ensure_index([("$**", "text")], name="TextIndex")

 # Get the collection stats (collstats) on a collection
 # named "mbox"
 print json.dumps(db.command("collstats", "mbox"), indent=1)

 # Use the db.command method to issue a "text" command
 # on collection "mbox" with parameters, remembering that
 # we need to use json_util to handle serialization of our JSON
 print json.dumps(db.command("text", "mbox",  
                        search="raptor", 
                        limit=1), 
             indent=1, default=json_util.default)

Tags: totextfromimportjsontruedbmongodb
1条回答
网友
1楼 · 发布于 2024-05-14 18:06:04

通常,textSearch是通过云数据库mongodb.conf启动mongodb时。请参阅:Enable Text Search文档。在

如果用户具有管理权限,也可以通过管理命令运行它:

client.admin.command('setParameter', textSearchEnabled=True)

相关问题 更多 >

    热门问题