How to convert a pymongo.cursor.Cursor into a dict?(如何将 pymongo.cursor.Cursor 转换为 dict?)
问题描述
我使用pymongo查询一个区域内的所有项目(实际上是查询地图上一个区域内的所有场地).我之前使用 db.command(SON()) 在球形区域中搜索,它可以返回给我一个字典,在字典中有一个名为 results 的键,其中包含场地.现在我需要在一个正方形区域中进行搜索,建议我使用 db.places.find,但是,这会返回一个 pymongo.cursor.Cursor 类,我有不知道如何从中提取场地结果.
有谁知道我是否应该将光标转换为字典并提取结果,或者使用其他方法查询方形区域中的项目?顺便说一句,db 是 pymongo.database.Database 类
代码是:
<预><代码>>>>进口pymongo>>>db = pymongo.MongoClient(host).PSRC>>>resp = db.places.find({"loc": {"$within": {"$box": [[ll_lng,ll_lat], [ur_lng,ur_lat]]}}})>>>对于相应的文档:>>>打印(文档)我有 ll_lng、ll_lat、ur_lng 和 ur_lat 的值,使用这些值但它不会从这些代码中打印任何内容
find 方法返回一个 Cursor 实例,它允许您遍历所有匹配的文档.
要获得符合给定条件的第一个文档,您需要使用 find_one.find_one 的结果是一个字典.
您始终可以使用 list 构造函数返回集合中所有文档的列表,但请记住,这会将所有数据加载到内存中,并且可能不是您想要的.
如果您需要重用游标并且有充分的理由不使用 rewind()
使用find的演示:
<小时>
使用find_one的演示:
I am using pymongo to query for all items in a region (actually it is to query for all venues in a region on a map). I used db.command(SON()) before to search in a spherical region, which can return me a dictionary and in the dictionary there is a key called results which contains the venues. Now I need to search in a square area and I am suggested to use db.places.find, however, this returns me a pymongo.cursor.Cursor class and I have no idea how to extract the venue results from it.
Does anyone know whether I should convert the cursor into a dict and extract the results out, or use another method to query for items in a square region? BTW, db is pymongo.database.Database class
The codes are:
>>> import pymongo
>>> db = pymongo.MongoClient(host).PSRC
>>> resp = db.places.find({"loc": {"$within": {"$box": [[ll_lng,ll_lat], [ur_lng,ur_lat]]}}})
>>> for doc in resp:
>>> print(doc)
I have values of ll_lng, ll_lat, ur_lng and ur_lat, use these values but it prints nothing from this codes
The find method returns a Cursor instance, which allows you to iterate over all matching documents.
To get the first document that matches the given criteria you need to use find_one. The result of find_one is a dictionary.
You can always use the list constructor to return a list of all the documents in the collection but bear in mind that this will load all the data in memory and may not be what you want.
You should do that if you need to reuse the cursor and have a good reason not to use rewind()
Demo using find:
>>> import pymongo
>>> conn = pymongo.MongoClient()
>>> db = conn.test #test is my database
>>> col = db.spam #Here spam is my collection
>>> cur = col.find()
>>> cur
<pymongo.cursor.Cursor object at 0xb6d447ec>
>>> for doc in cur:
... print(doc) # or do something with the document
...
{'a': 1, '_id': ObjectId('54ff30faadd8f30feb90268f'), 'b': 2}
{'a': 1, 'c': 3, '_id': ObjectId('54ff32a2add8f30feb902690'), 'b': 2}
Demo using find_one:
>>> col.find_one()
{'a': 1, '_id': ObjectId('54ff30faadd8f30feb90268f'), 'b': 2}
这篇关于如何将 pymongo.cursor.Cursor 转换为 dict?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 pymongo.cursor.Cursor 转换为 dict?
基础教程推荐
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
