[英]PyMongo text search using aggregation
I'm trying a simple text search using the aggregation framework. 我正在尝试使用聚合框架进行简单的文本搜索。 I'm using Mongo 3.0.7
我正在使用Mongo 3.0.7
The database has a field named 'text'. 该数据库具有一个名为“文本”的字段。 I'm trying to retrieve the data from mongo using python.
我正在尝试使用python从mongo检索数据。 I'm using the code below.
我正在使用下面的代码。 Please correct me if i'm wrong.
如果我错了,请纠正我。
db.tweets.create_index({"text":"text"})
cursor=db.tweets.aggregate(([{"match": {"text": {"search": "cake"}}}]))
for document in cursor:
print(document)
I get the error : planner returned error: need exactly one text index for $text query. 我收到错误消息:规划器返回错误:$ text查询需要一个文本索引。
Thanks! 谢谢!
Resolved it, i was unaware of the concept 解决它,我不知道这个概念
BSON is designed to be traversed easily. BSON被设计为易于遍历。 This is a vital property in its role as the primary data representation for MongoDB.
作为MongoDB的主要数据表示形式,这是至关重要的属性。
My above code was generating in BSON format which is not in a human readable format. 我上面的代码以BSON格式生成,而不是人类可读的格式。 The mistake I made was to display it with a simple print.
我犯的错误是要以简单的印刷形式显示它。
Solution : I need to convert the BSON object to JSON and then try to traverse through the list. 解决方案:我需要将BSON对象转换为JSON,然后尝试遍历列表。
from bson import json_util
json_docs = [json.dumps(document , default=json_util.default) for document in cursor ]
for jsondump in json_docs:
x=json.loads(jsondump)
print(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.