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. Unity入门知识

    参考书:<Unity3D 游戏开发> ● scene图中按F键:放大,居中当前选中的物体 ● 坐标轴:红-x轴,绿-y轴,蓝-z轴 ● 逐帧运行程序: ● OnGUI:可以用来画界面 ● ...

  2. ISV 和SI 是什么

    ISV是Independent Software Vendors 的英文缩写,意为"独立软件开发商",特指专门从事软件的开发.生产.销售和服务的企业,如微软(Microsoft). ...

  3. Dom之表单提交与默认行为

    一.button提交表单 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  4. html之ul标签

    html之无序列表,建议使用样式来定义列表的类型. 通常和li配对使用 可选属性: type:disc 圆点,circle圆圈,square方块 compact:显示效果比正常更小巧 <body ...

  5. HadoopDoctor:来自腾讯数据仓库TDW的MR诊断系统

    TDW是基于Hadoop生态圈研发的大数据处理平台,MapReduce计算引擎在TDW平台中承担了所有的离线数据计算,是TDW最重要的底层支撑平台之一.在TDW 平台中,除了MR程序会生成MapRed ...

  6. centos6.5 安装iptables

    阿里云默认是没有安装iptables 安装 yum install -t iptables yum install iptables-services 检查iptables服务的状态 service ...

  7. 转(linux shell)(2)

      http://oldboy.blog.51cto.com/2561410/1665163 1.按单词出现频率降序排序! 2.按字母出现频率降序排序! the squid project provi ...

  8. 用Opera Mobile调试手机版网页【转】

    注意:新版本的opera已经采用webkit内核,没有dragonfly了. 要下载12版的http://get.geo.opera.com/pub/opera/win/1216/int/Opera_ ...

  9. LintCode "Previous Permutation"

    A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy ...

  10. FullCalendar

    一个非常完美的日期控件:https://fullcalendar.io/