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. ...
随机推荐
- MySQL 常用show命令
MySQL中有很多的基本命令,show命令也是其中之一,在很多使用者中对show命令的使用还容易产生混淆,本文汇集了show命令的众多用法. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- Unity3d 无网络权限下打开网站
有人问“更多游戏”没有网络权限怎么实现,其实调用浏览器访问外部链接不需要网络通讯权限,代码如下: Uri moreGame = Uri.parse("http://wapgame.189.c ...
- Python_day8_面向对象(多态、成员修饰符、类中特殊方法、对象边缘知识)、异常处理之篇
一.面向对象之多态 1.多态:简而言子就是多种形态或多种类型 python中不支持多态也用不到多态,多态的概念是应用与java/C#中指定传参的数据类型, java多态传参:必须是传参数的数据类型或传 ...
- java_常用数据类型转换基础篇
一.java基本数据类型 1.java基本数据类型可分四类八中 第一类:整形:byte.short.int.long 第二类:浮点型:float(单精度) .double(双精度) 第三类:逻辑类型: ...
- 自定义Mvc5 Owin 验证
public class AuthIn : IUserAuthenticate { public static ApplicationUserManager UserManager { get { r ...
- P1027 木瓜地
/*=========================================================== 描述 Description Bessie不小心游荡出Farmer John ...
- Intent的详细解析以及用法
Intent的详细解析以及用法 Android的四大组件分别为Activity .Service.BroadcastReceiver(广播接收器).ContentProvider(内容提供者 ...
- java指针
import java.util.ArrayList; import java.util.List; public class TestPoint { public static void main( ...
- ajax简单案例:返回json型
主页: <!--输入代号点击查询查到本代号的人名--> <body> <div> 请输入代号:<input type="text" id= ...
- Struts2 - Rest(1)
Struts2提供了一个restful的插件:struts2-rest-plugin-2.3.16.1.jar 这个插件可以把Struts2当做restful来使用,不过它的rest功能目前来说有点“ ...