mongodb的python接口pymongo使用
1. 连接
from pymongo import MongoClient
client = MongoClient("mongodb://mongodb0.example.net:27019")
# client = MongoClient() db = client['primer'] coll = db.dataset
# coll = db['dataset']
2. 插入
from datetime import datetime
result = db.restaurants.insert_one(
{
"address": {
"street": "2 Avenue",
"zipcode": "",
"building": "",
"coord": [-73.9557413, 40.7720266]
},
"borough": "Manhattan",
"cuisine": "Italian",
"grades": [
{
"date": datetime.strptime("2014-10-01", "%Y-%m-%d"),
"grade": "A",
"score":
},
{
"date": datetime.strptime("2014-01-16", "%Y-%m-%d"),
"grade": "B",
"score":
}
],
"name": "Vella",
"restaurant_id": ""
}
)
3. 查找
cursor = db.restaurants.find({"address.zipcode": ""})
for document in cursor:
print(document)
cursor = db.restaurants.find(
{"$or": [{"cuisine": "Italian"}, {"address.zipcode": "10075"}]})
4. 排序输出
import pymongo
cursor = db.restaurants.find().sort([
("borough", pymongo.ASCENDING),
("address.zipcode", pymongo.DESCENDING)
])
5. 更新
result = db.restaurants.update_many(
{"address.zipcode": "", "cuisine": "Other"},
{
"$set": {"cuisine": "Category To Be Determined"},
"$currentDate": {"lastModified": True}
}
)
print result.modified_count
10
6. 删除
result = db.restaurants.delete_many({"borough": "Manhattan"})
print result.deleted_count
7. 聚合
Group Documents by a Field and Calculate Count
Use the $group stage to group by a specified key. In the $group stage, specify the group by key in the _id field. $group accesses fields by the field path, which is the field name prefixed by a dollar sign $. The $group stage can use accumulators to perform calculations for each group. The following example groups the documents in the restaurants collection by the borough field and uses the $sum accumulator to count the documents for each group.
cursor = db.restaurants.aggregate(
[
{"$group": {"_id": "$borough", "count": {"$sum": }}}
]
)
mongodb的python接口pymongo使用的更多相关文章
- python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查
python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...
- MongoDB的Python客户端PyMongo(转)
原文:https://serholiu.com/python-mongodb 这几天在学习Python Web开发,于是做准备做一个博客来练练手,当然,只是练手的,博客界有WordPress这样的好玩 ...
- MongoDB与python交互
1.Pymongo PyMongo是Mongodb的Python接口开发包,是使用python和Mongodb的推荐方式.官方文档 2.安装 进入虚拟环境 sudo pip install pymon ...
- Python 使用pymongo操作mongodb库
Python 使用pymongo操作mongodb库 2016-12-31 21:55 1115人阅读 评论(0) 收藏 举报 分类: - - - Python(10) 版权声明:本文为博主原创文 ...
- ❤️Python接口自动化,一文告诉你连接各大【数据库】建议收藏❤️
@ 目录 前言 常见数据库 Mysql Oracle sql-server PostgreSQL MongoDB Redis 前言 相信很多小伙伴在使用python进行自动化测试的时候,都会涉及到数据 ...
- 基于mongodb的python之增删改查(CRUD)
1,下载mongodb的python驱动,http://pypi.python.org/pypi/pymongo/,根据操作系统和python平台版本选择相应的egg或exe安装. 2,新建一个py脚 ...
- 【Python】pymongo使用
官方文档:http://api.mongodb.com/python/current/index.html MongoReplicaSetClient:http://api.mongodb.com/p ...
- python之pymongo
引入 在这里我们来看一下Python3下MongoDB的存储操作,在本节开始之前请确保你已经安装好了MongoDB并启动了其服务,另外安装好了Python的PyMongo库. MongoDB 数据库安 ...
- 7.mongo python 库 pymongo的安装
1.Python 中如果想要和 MongoDB 进行交互就需要借助于 PyMongo 库,在CMD中使用命令即可[注意此处是pip3,pip无法安装]: pip3 install pymongo 2. ...
随机推荐
- Intel MKL函数,如何得到相同的计算结果?【转】
在运行程序时,我们总希望多次运行的结果,是完全一致,甚至在不同的机器与不同的OS中,程序运行的结果每一位都完全相同. 事实上,程序往往很难保证做到这一点. 为什么呢? 我们先看一个简单的例子: 当程序 ...
- 怎么用ABBYY创建属于自己的PDF
怎么创建一份属于自己的PDF文档呢?由于PDF格式文件具有跨平台.支持超长文件.安全可靠性高等诸多优势,在日常办公学习中应用越来越广泛.而随着技术的发展,各种办公软件也对PDF提供越来越多的支持,但P ...
- snort-2.9.7.0源码安装过程
2015/02/15,centos6.5-64-minimal,初始205个包 [root@localhost snort]# yum install wget[root@localhost snor ...
- unity, collider/trigger on children
参考:http://answers.unity3d.com/questions/410711/trigger-in-child-object-calls-ontriggerenter-in-pa.ht ...
- 递归函数练习:输出菲波拉契(Fibonacci)数列的前N项数据
/*====================================================================== 著名的菲波拉契(Fibonacci)数列,其第一项为0 ...
- Oboe 提升web 用户体验以及性能
Oboe 地址:http://oboejs.com/ 1.安装 bower bower install oboe 2.使用,ajax 模式 oboe('/myapp/things.json') ...
- 通过CSS禁用页面模块的复制和粘贴功能
样式代码: -moz-user-select: none; -webkit-user-select: none; -ms-user-select: none; -khtml-user-select: ...
- 使用Axure制作App原型应该怎样设置尺寸?
使用Axure制作的原型,如果你没有设置自适应视图的话它是不会自动适应任何设备的. 若要解释清楚这个问题需要的篇幅比较长,请大家自行参考 Point/Pixel/PPI/DPI 的意思和它们之间的关系 ...
- Knockout应用开发指南(完整版) 目录索引
http://learn.knockoutjs.com/ 所有示例和代码都在在上面直接运行预览 注意:因为它用了google的cdn加速,所要要用代_理+_翻_墙才能正常加载 使用Knockout有 ...
- Oracle “CONNECT BY” 使用 [转]
Oracle “CONNECT BY”是层次查询子句,一般用于树状或者层次结果集的查询.其语法是: 1 [ START WITH condition ]2 CONNECT BY [ NOCYCLE ] ...