【问题标题】:PyMongo text search using aggregation使用聚合的 PyMongo 文本搜索
【发布时间】:2016-03-06 02:12:01
【问题描述】:

我正在尝试使用聚合框架进行简单的文本搜索。我正在使用 Mongo 3.0.7

数据库有一个名为“文本”的字段。我正在尝试使用 python 从 mongo 检索数据。我正在使用下面的代码。如果我错了,请纠正我。

db.tweets.create_index({"text":"text"})
cursor=db.tweets.aggregate(([{"match": {"text": {"search": "cake"}}}]))

for document in cursor:
  print(document)

我收到错误:规划器返回错误:$text 查询需要一个文本索引。

谢谢!

【问题讨论】:

    标签: python mongodb python-3.x mongodb-query pymongo


    【解决方案1】:

    解决了,我不知道这个概念

    BSON 旨在轻松遍历。作为 MongoDB 的主要数据表示,这是一个至关重要的属性。

    我上面的代码是以 BSON 格式生成的,它不是人类可读的格式。我犯的错误是用简单的打印来显示它。

    解决方案:我需要将 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)
    

    【讨论】:

      猜你喜欢
      • 2016-10-14
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 2019-07-03
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多