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使用的更多相关文章

  1. python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查

    python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...

  2. MongoDB的Python客户端PyMongo(转)

    原文:https://serholiu.com/python-mongodb 这几天在学习Python Web开发,于是做准备做一个博客来练练手,当然,只是练手的,博客界有WordPress这样的好玩 ...

  3. MongoDB与python交互

    1.Pymongo PyMongo是Mongodb的Python接口开发包,是使用python和Mongodb的推荐方式.官方文档 2.安装 进入虚拟环境 sudo pip install pymon ...

  4. Python 使用pymongo操作mongodb库

    Python 使用pymongo操作mongodb库 2016-12-31 21:55 1115人阅读 评论(0) 收藏 举报  分类: - - - Python(10)  版权声明:本文为博主原创文 ...

  5. ❤️Python接口自动化,一文告诉你连接各大【数据库】建议收藏❤️

    @ 目录 前言 常见数据库 Mysql Oracle sql-server PostgreSQL MongoDB Redis 前言 相信很多小伙伴在使用python进行自动化测试的时候,都会涉及到数据 ...

  6. 基于mongodb的python之增删改查(CRUD)

    1,下载mongodb的python驱动,http://pypi.python.org/pypi/pymongo/,根据操作系统和python平台版本选择相应的egg或exe安装. 2,新建一个py脚 ...

  7. 【Python】pymongo使用

    官方文档:http://api.mongodb.com/python/current/index.html MongoReplicaSetClient:http://api.mongodb.com/p ...

  8. python之pymongo

    引入 在这里我们来看一下Python3下MongoDB的存储操作,在本节开始之前请确保你已经安装好了MongoDB并启动了其服务,另外安装好了Python的PyMongo库. MongoDB 数据库安 ...

  9. 7.mongo python 库 pymongo的安装

    1.Python 中如果想要和 MongoDB 进行交互就需要借助于 PyMongo 库,在CMD中使用命令即可[注意此处是pip3,pip无法安装]: pip3 install pymongo 2. ...

随机推荐

  1. OCR文字识别软件 怎么识别包含非常规符号的文本

    ABBYY FineReader 12 是一款OCR图文识别软件,可快速方便地将扫描纸质文档.PDF文件和数码相机的图像转换成可编辑.可搜索的文本,有时文本中可能会包含一些非常规的符号,此时ABBYY ...

  2. Google将数十亿行代码储存在单一的源码库

    过去16年,Google使用一个中心化源码控制系统去管理一个日益庞大的单一共享源码库.它的代码库包含了约10亿个文件(有重复文件和分支)和 3500万行注解,86TB数据,900万唯一源文件中含有大约 ...

  3. 【性能诊断】十一、性能问题综合分析(案例2,windbg、wireshark)

    [问题描述]:       前段时间有一项目反馈,常用的审批功能有时的响应较慢,多个管理员功能不定期的出现客户端无响应的状况,并且管理员功能一旦出现卡死,也会影响到普通的业务用户致使很多用户无法操作. ...

  4. ASP.NET MVC几种找不到资源的问题解决办法

    转自:http://www.cnblogs.com/xyang/archive/2011/11/24/2262003.html 在MVC中,controller中的Action和View中的.csht ...

  5. HTML 元素

    HTML 文档是由 HTML 元素定义的. HTML 元素 HTML 元素指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 开始标签 元素内容 结束标签 <p> ...

  6. [转]java生成随机数字和字母组合

    摘自 http://blog.csdn.net/xiayaxin/article/details/5355851 import java.util.Random; public String getC ...

  7. 理解javascript中的原型模式

    一.为什么要用原型模式. 早期采用工厂模式或构造函数模式的缺点:  1.工厂模式:函数creatPerson根据接受的参数来构建一个包含所有必要信息的person对象,这个函数可以被无数次的调用,工厂 ...

  8. 访问本机的WEB API 报400错误

    当时用的IP是127.0.0.1 报400错误,换成 localhost 后正常.

  9. Java 提示 JRE unbound 或者 Tomcat unbound 解决方法

    解决步骤 1.右键你的项目>>build path>>config build path 2. 选中显示 unbound的 Libraries >>Edit 如下图 ...

  10. Redis资料汇总专题

    1.Redis是什么? 十五分钟介绍 Redis数据结构 Redis系统性介绍 一个很棒的Redis介绍PPT 强烈推荐!非同一般的Redis介绍 Redis之七种武器 锋利的Redis redis ...