-------------------python调用MongoDB-------------------
1、官方文档:http://api.mongodb.org/python/current/tutorial.html
 
2、linux下安装指令:sudo pip install pymongo
 
3、测试python驱动:
 #coding=utf-8

 '''
测试python驱动
''' #引用对应的包
import pymongo #创建一个mongo客户端对象
client = pymongo.MongoClient("127.0.0.1",27017)
#获得mongoDB中的数据库对象
db = client.test_database
#在数据库中创建一个集合
collection = db.test_collectionTwo #创建一条要加入数据库中的数据信息,json格式
post_data = {"username":"xiaohao","pwd":"",} #进行数据的添加操作,inserted_id返回添加后的id编号信息
post_data_id = collection.insert_one(post_data).inserted_id #展示一下插入的数据信息
print collection.find_one() #打印当前数据库中所有集合的民称
print db.collection_names() #打印当前集合中数据的信息
for item in collection.find():
print item #打印当前集合中数据的个数
print collection.count() #进行find查询,并打印查询到的数据的条数
print collection.find({"username":"xiaohao"}).count()

4、Aggregation Examples:mongoDB聚合练习

 #coding=utf-8

 '''
进行聚合aggregation 练习
''' #引包
import pymongo client = pymongo.MongoClient("127.0.0.1",27017) db = client.aggregation_database collection = db.aggregation_collection collection.insert_many([
{"username":"xiaohao01","pwd":""},
{"username":"xiaohao02","pwd":""},
{"username":"xiaohao03","pwd":""}
]) # for item in collection.find():
# print item #python中不含有son语法,需要使用son from bson.son import SON pipeline = [
{"$unwind":"$pwd"},
{"$group":{"_id":"pwd","count":{"$sum":1}}},
{"$sort":SON([("count",-1),("_id",-1)])}
] result = list(collection.aggregate(pipeline)) print result

MongoDB-python的API手记的更多相关文章

  1. MongoDB Python官方驱动 PyMongo 的简单封装

    最近,需要使用 Python 对 MongodB 做一些简单的操作,不想使用各种繁重的框架.出于可重用性的考虑,想对 MongoDB Python 官方驱动 PyMongo 做下简单封装,百度一如既往 ...

  2. 用 Identity Server 4 (JWKS 端点和 RS256 算法) 来保护 Python web api

    目前正在使用asp.net core 2.0 (主要是web api)做一个项目, 其中一部分功能需要使用js客户端调用python的pandas, 所以需要建立一个python 的 rest api ...

  3. mongoDB python 操作

    mongoDB python 操作 import pymongo mongo_client = pymongo.MongoClient(host="127.0.0.1",port= ...

  4. Python DB API 连接数据库

    Python DB API Mysql,Oracle,SqlServer 不关闭,会浪费资源.

  5. Python调用API接口的几种方式 数据库 脚本

    Python调用API接口的几种方式 2018-01-08 gaoeb97nd... 转自 one_day_day... 修改 微信分享: 相信做过自动化运维的同学都用过API接口来完成某些动作.AP ...

  6. Python调用API接口的几种方式

    Python调用API接口的几种方式 相信做过自动化运维的同学都用过API接口来完成某些动作.API是一套成熟系统所必需的接口,可以被其他系统或脚本来调用,这也是自动化运维的必修课. 本文主要介绍py ...

  7. Python Elasticsearch api,组合过滤器,term过滤器,正则查询 ,match查询,获取最近一小时的数据

    Python Elasticsearch api   描述:ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.下 ...

  8. 如何用 Python 和 API 收集与分析网络数据?

    摘自 https://www.jianshu.com/p/d52020f0c247 本文以一款阿里云市场历史天气查询产品为例,为你逐步介绍如何用 Python 调用 API 收集.分析与可视化数据.希 ...

  9. ServerLess & MongoDB Atlas & REST API

    ServerLess & MongoDB Atlas & REST API ServerLess, Nodejs, MongoDB Atlas cloud 构建 REST API ht ...

随机推荐

  1. Spring源码情操陶冶-AbstractApplicationContext#initMessageSource

    承接前文Spring源码情操陶冶-AbstractApplicationContext#registerBeanPostProcessors 约定web.xml配置的contextClass为默认值X ...

  2. http post,get,put,delete区别(收集整理)

    摘要: 这篇文章想从restful角度来分析下http的方法get,post,put,delete的区别.先暂时收集下资料~~~ 一 Http 规范中的get与post 根据 HTTP 规范,GET  ...

  3. javascript编程解决黑白卡片问题

    问题描述: 时间限制:1秒 空间限制:32768K 牛牛有n张卡片排成一个序列.每张卡片一面是黑色的,另一面是白色的.初始状态的时候有些卡片是黑色朝上,有些卡片是白色朝上.牛牛现在想要把一些卡片翻过来 ...

  4. rabbitmq重装之后无法加入原有cluster的bug解析

    背景: 一台controller node,一台compute1节点 两台机器的host文件均已经进行hostname解析 两节点本已经加入了同一rabbitmq cluster 但controlle ...

  5. 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)

    22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...

  6. C++ STL map详解

    一.解释: p { margin-bottom: 0.25cm; direction: ltr; color: #00000a; line-height: 120%; text-align: just ...

  7. offsetWidth\offsetleft 等图文解释

      网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offs ...

  8. python 分支语句 循环语句

    分支语句 #if-else if a > b: print('aaa') else: print('bbb') #if-elif-else if a > b: print('a>b' ...

  9. Zabbix监控IO

    导入模板 configuration->templates->import,选择你需要导入的模板文件 #zabbix_agentd配置 # iostat #磁盘读的次数 UserParam ...

  10. 阿里消息队列中间件 RocketMQ 源码分析 —— Message 拉取与消费(上)