Install

pymongo document

install pymongo from the tar package download from website

python setup.y install

Usage

# import
from pymongo import MongoClient # Making a Connection with MongoClient client = MongoClient(host='127.0.0.1', port=27017) # use the MongoDB URI format: # client = MongoClient('mongodb://localhost:27017/') # getting a database
db_mark = client['mark'] # getting a collection
c_student = db_mark.student # getting all of the documents in collection c_student
students = c_student.find()
for student in students:
print(student)

Insert

The three methods to insert data to collection, obviously, insert() method is deprecated.

# Insert an iterable of documents
stu.insert_many(students) # insert is deprecated. Use insert_one or insert_many instead
stu.insert() # Insert a single document
stu.insert_one()
from pymongo import MongoClient

client = MongoClient('mongodb://localhost:27017/')

db = client['python']
# stu = db.create_collection('stu')
stu = db.stu students = [
{'name': 'jackson', 'age': 17, 'gender': 1},
{'name': 'amada', 'age': 19, 'gender': 0},
{'name': 'micheal', 'age': 23, 'gender': 1},
{'name': 'lucy', 'age': 16, 'gender': 0},
{'name': 'happy', 'age': 12, 'gender': 1},
{'name': 'java', 'age': 34, 'gender': 0},
{'name': 'python', 'age': 21, 'gender': 1},
{'name': 'ruby', 'age': 11, 'gender': 1},
] stu.insert_many(students) #stu.insert()
#stu.insert_one()

Delete

stu.delete_many()  # justOne:False
stu.delete_one() # justOne:True

Update

stu.update()
stu.update_many()
stu.update_one()

Search

find()

stu.find()
stu.find_one()
stu.find_one_and_replace()
stu.find_one_and_delete()
stu.find_one_and_update()
# show total number
stu.count()

aggregate()

In [22]: s.aggregate([{'$project':{'_id':0}}])
Out[22]: <pymongo.command_cursor.CommandCursor at 0x7efdda495a20> In [23]: ret=s.aggregate([{'$project':{'_id':0}}]) In [24]: for x in ret:
...: print(x)
...:
{'name': 'jack', 'age': 12, 'gender': 1}
{'name': 'rose', 'age': 11, 'gender': 0}
{'name': 'jackson', 'age': 17, 'gender': 1}
{'name': 'amada', 'age': 19, 'gender': 0}
{'name': 'micheal', 'age': 23, 'gender': 1}
{'name': 'lucy', 'age': 16, 'gender': 0}
{'name': 'happy', 'age': 12, 'gender': 1}

End

fatal method to serach data from mongodb database is use aggregate

The Usage of Pymongo的更多相关文章

  1. Python: Windows 7 64位 安装、使用 pymongo 3.2

    官网tutorial:  http://api.mongodb.com/python/current/tutorial.html 本教程将要告诉你如何使用pymongo模块来操作MongoDB数据库. ...

  2. intellij IDEA 出现“Usage of API documented as @since 1.6+”的解决办法

    问题 在导入java.io.console的时候出现"Usage of API documented as @since 1.6+"

  3. Disk Space Usage 术语理解:unallocated, unused and reserved

    通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...

  4. OpenCascade MeshVS Usage

    OpenCascade MeshVS Usage eryar@163.com Abstract. MeshVS means Mesh Visualization Service. It can be ...

  5. 2.0 (2)测试pymongo

    在数据库中创建数据库.表,插入数据. from pymongo import MongoClient host = "localhost" port = 27017 client ...

  6. Windows平台下为Python添加MongoDB支持PyMongo

    到Python官网下载pymongo-2.6.3.win-amd64-py2.7.exe 安装pymongo-2.6.3.win-amd64-py2.7.exe 参照官方的用例进行测试 打开命令提示符 ...

  7. Usage: AddDimensionedImage imageFile outputFile eclipse 运行程序出错

    关于这个在eclipse中运行java程序的错,首先确认你的jdk,jre是否完整,并且与你的eclipse的位数相同,当然我相信这个错误大家应该都会去检查到. 第二个关于addDimensioned ...

  8. Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(info)

    本文转载自:http://blog.csdn.net/myfxx/article/details/21096949 今天在用eclipse启动项目的时候发现了一个问题,就是每次启动项目的时候,ecli ...

  9. [转]Dynamic SQL & Stored Procedure Usage in T-SQL

    转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...

随机推荐

  1. SQL 从入门到 DBA 删库跑路

    SQL 从入门到 DBA 删库跑路 一.基础 人员信息表: ID 姓名 性别 出生 婚否 学历 工资 工会 35009449 孙xx 男 1978-2-17 未婚 中专 3000 TRUE 35000 ...

  2. ORACLE高级部分内容

    1.pl/sql基本语句 DECLARE BEGIN END; / 循环语句 DECLARE I  NUMBER(2):=1; BEGIN WHILE I<100 LOOP I:=I+1; EN ...

  3. OSS文件上传及OSS与ODPS之间数据连通

    场景描述        有这样一种场景,用户在自建服务器上存有一定数量级的CSV格式业务数据,某一天用户了解到阿里云的OSS服务存储性价比高(嘿嘿,颜值高),于是想将CSV数据迁移到云上OSS中,并且 ...

  4. NOIP2019普及级别模拟 3.30校模拟

    好吧我还是第一次写这种总结类的玩意… 考场心情…hmm…我没睡醒.是的是这样的,反正题都有两三个看错了或者没看懂… 最关键的是!!我!居!然!把!Freopen!写!在!了!程!序!最!后! 然后就和 ...

  5. 【转载】inotify+rsync实时同步 解决同步慢问题 (转载备记)

    原文地址:http://www.ttlsa.com/web/let-infotify-rsync-fast/ 背景 我们公司在用inotify+rsync做实时同步,来解决分布式集群文件一致性的问题. ...

  6. 用命令从mysql中导出/导入表结构及数据

    在命令行下mysql的数据导出有个很好用命令mysqldump,它的参数有一大把,可以这样查看:mysqldump最常用的:mysqldump -uroot -pmysql databasefoo t ...

  7. js经典试题之数据类型

    js经典试题之数据类型 1:输出"B" + "a" + + "B" + "a"的值: 答案:BaNaNa. 分析:因为+ ...

  8. puppet学习笔记

    puppet优势:容易理解.用户较多.门槛低.简单.安装配置文件较少 puppet使用Ruby语言开发,安装puppet需要安装Ruby puppet运行环境:Redhat.Centos.Window ...

  9. bootstrapValidator.js,最好用的bootstrap表单验证插件 简单实用方法

    实用方法 1.引入 在有jquery和bootstrap的页面里引入bootstrapValidator.js和bootstrapValidator.css文件 2. 按照bootstrap的表单组件 ...

  10. how to install pygraphviz on windows 10 with python 3.6

    Here's what worked for me: Win 7 AMD64 Install MSFT C++ compiler. Install Anaconda for Win AMD64, Py ...