- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我是用pymongo来查询一个地区的所有元素(其实是在一张 map 上查询一个地区的所有 field )。我之前使用 db.command(SON())
在球形区域中搜索,它可以返回一个字典,并且在字典中有一个名为 results
的键,其中包含场馆。现在我需要在一个正方形区域中搜索,建议我使用 db.places.find
,但是,这会返回一个 pymongo.cursor.Cursor
类,我有不知道如何从中提取 field 结果。
有谁知道我应该将光标转换为字典并提取结果,还是使用其他方法查询方形区域中的项目?顺便说一句,db 是 pymongo.database.Database 类
代码是:
>>> 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)
我有 ll_lng、ll_lat、ur_lng 和 ur_lat 的值,使用这些值但它不会从这些代码中打印任何内容
最佳答案
find
方法返回 Cursor
实例,它允许您遍历所有匹配的文档。
要获取第一个符合给定条件的文档,您需要使用 find_one
. find_one
的结果是一个字典。
您始终可以使用 list
构造函数返回集合中所有文档的列表,但请记住,这会将所有数据加载到内存中,并且可能不是您想要的。
如果您需要重用光标并且有充分的理由不使用 rewind()
,您应该这样做
使用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}
使用 find_one
的演示:
>>> col.find_one()
{'a': 1, '_id': ObjectId('54ff30faadd8f30feb90268f'), 'b': 2}
关于python - 如何将 pymongo.cursor.Cursor 转换为字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28968660/
我正在尝试扩展我的第一个 Flask 应用程序,但不了解在多个模块中使用 pymongo 数据库所需的结构。例如,这是我的新结构: run.py app/ ├── __init__.py ├── fo
Pymongo 返回一个游标,我可以迭代结果并将其文档附加到列表中。有没有办法直接获取列表中的结果文档?谢谢 最佳答案 以下代码会将整个结果集(光标)转换为列表: myresults = list(m
我使用pip3成功安装了pymongo Aleeshas-MacBook-Air:project 2 aleesha$ sudo pip3 install pymongo The directory
我正在尝试运行非常简单的代码来弄清楚如何将 pymongo 与 MongoDB Atlas Cloud 结合使用。 这是示例代码 import pymongo client = pymongo.Mon
我想找到集合 (mycol) 中的文档的 _id,其中 "name":"John"。我已插入文档,但想找到文档的 _id。可能吗 ?我正在尝试 result = db.mycol.find({"_id
我是 PyMongo 和 Flask 的新手,不过我已经完成了 Flask 的教程,并且对它感觉很舒服。我现在正在尝试使用 MongoDb 实现 flask 服务器,但我不确定如何进行。 我看到有两个
我正在尝试在 mongodb 集合中批量插入一些文档。 我对我收藏的链接字段施加了唯一约束。 bulkUrls = db.urls.initialize_ordered_bulk_op() for i
简单介绍一下背景知识,我之前曾在 Node.js 和 mongoose.js 中使用过 MongoDB。现在我决定尝试使用 python 和 pymongo。但是,当我尝试将文档插入到我的集合中时,我
我正在获取 MongoDB 中某个集合的全部数据,一段时间后(比如 30 或 60 分钟),脚本会引发以下错误: pymongo.errors.CursorNotFound: cursor id 18
我在 mongodb 数据库中消费了一堆推文。我想使用 pymongo 查询这些推文。例如,我想查询 screen_name。但是,当我尝试这样做时,python 不会返回推文,而是返回有关 pymo
我有一个 合作伙伴集合,我正在使用 pymongo 来检索数据 当我使用 MongoDB 查询集合时,我看到以下结果 db.partner.find({'unique_key': 'c89dbe313
我正在尝试使用 Pymongo 将字典插入到 MongoDB 集合中。代码段为 newdict = {'id': a, 'usr_id': i.get('user_id'), 'reach_value
我尝试使用 pymongo 连接 MongoDB。但是遇到了dnspython必须安装的错误即使在我安装了 pymongo 和 dnspython 之后。 我的代码是: import pymongo
我正在使用 pymongo 将数亿条格式为 {'id_str': , 'created_at': , 'text': } 的推文从文本文件迁移到 MongoDB。为每个用户创建一个集合来存储他/她的推
我知道这是一个相当普遍的问题。我正在编写一个小型 Flask 应用程序,并试图将一些查询反馈给 View 。 我已经连接到我的本地 MongoDB 设置,并进行了成功的查询 - 但我无法用它生成 js
我想从 ObjectId 对象中获取字符串字符。我用的是pymongo。例如:ObjectId("543b591d91b9e510a06a42e2"),我想获取"543b591d91b9e510a06
我想实现一个函数,该函数需要以与插入相反的顺序从 pymongo 集合中获取值。 我可以想到几个方法: cursor = collection.find(skip=collection.count()
从pymongo文档: MongoDB以BSON格式存储数据。 BSON字符串采用UTF-8编码,因此PyMongo必须确保 它存储的任何字符串仅包含有效的UTF-8数据。常规字符串()>已验证,并且
目录 查询数据 设置查询条件 更多查询操作 PS:pymongo最大查询限制 解决方案 查询数据 往
修订问题。将很快更新。 最佳答案 演示 - https://mongoplayground.net/p/ksay82IaGHs 按 TeacherID 分组和 TeacherID并获得组合的出现,$s
我是一名优秀的程序员,十分优秀!