-------------------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

最新文章

  1. nginx 301 永久重定向
  2. 嵌入式Linux驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动
  3. knockoutJS学习笔记03:knockout简介
  4. percona-toolkit中在线ddl
  5. 网络安全之PHP安全编程建议
  6. 使用apt-get方式为Kubuntu安装PHP+MYSQL+Apache
  7. 安装程序无法复制文件 convlog.exe的解决方法
  8. [linux]查看文件编码和编码转换
  9. IOS开发一些资源收集
  10. MySQL计划任务(事件调度器)(Event Scheduler)
  11. java多线程之停止线程
  12. windows下使用eclipse调试C程序
  13. 练习2 D 题- 第几天?
  14. ios在项目中打开word文档、ppt等总结
  15. Azure 基础:Table storage
  16. ORA-01031:insufficient privileges解决方法
  17. [Linux]使用awk批量杀进程的命令
  18. vim多行增加缩进
  19. Docker: docker 启动一个Nginx容器
  20. CentOS 下安装 Node.js 8.11.3 LTS Version

热门文章

  1. tomcat的常用配置方法
  2. An internal error occurred during: "Launching web on MyEclipse Tomcat"
  3. Vue组件基础用法
  4. Cocoapods 应用第二部分-私有库相关
  5. Quartus16.0如何使用TCL脚本
  6. CentOS6 安装Sendmail + Dovecot + Squirrelmail
  7. UIButton 中高亮取消
  8. 米扑代理示例(mimvp-proxy-demo)
  9. Hibernate(四)
  10. MySQL连接问题【mysql_connect和mysql_pconnect区别】