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. Linux命令应用大词典-第9章 数字计算

    9.1 bc:任意精度的计算器 9.2 dc:一个任意精度的计算器 9.3 expr:将表达式的值打印到标准输出 9.1 bc:任意精度的计算器 9.2 dc:一个任意精度的计算器 9.3 expr: ...

  2. Java基础知识:Java实现Map集合二级联动3

    * Returns an image stored in the file at the specified path * @param path String The path to the ima ...

  3. 【QT】宏

    宏 Q_CORE_EXPORT _CORE_EXPORT 其实是一个宏,用来说明这是一个动态库导出类.QT是个跨平台的库,而不同的操作系统,不同的编译器,对动态库的导出说明是不一样的,比如,在wind ...

  4. Python3 Tkinter-Text

    1.创建 from tkinter import * root=Tk() t=Text(root) t.pack() root.mainloop() 2.添加文本 from tkinter impor ...

  5. Python3 Tkinter-Spinbox

    1.创建 from tkinter import * root=Tk() Spinbox(root).pack() root.mainloop() 2.参数 from_    最小值 to    最大 ...

  6. (转载)IE8+兼容经验小结

    本文分享下我在项目中积累的IE8+兼容性问题的解决方法.根据我的实践经验,如果你在写HTML/CSS时候是按照W3C推荐的方式写的,然后下面的几点都关注过,那么基本上很大一部分IE8+兼容性问题都OK ...

  7. Zen Coding && Emmet-Sublime 安装

    Sublime Text 插件之:Emmet,旧版称:ex-Zen Coding 更名之后增加了CSS3和HTML5许多新特性.项目地址也从 code.google 移 github. 安装: Pac ...

  8. “Hello world!”团队第二周贡献分规则+贡献分数分配结果

    一.贡献规则制定: (1)基础分:9 , 9 , 8 , 7 , 7 , 7 , 6(按在本次编程中承担模块的重要度制定,某一模块重要度的认定通过组内开会讨论决定) (2)会议分:每人没出勤一次会议记 ...

  9. 异常概念和处理机制,try-catch-finally,throw和throws,自定义异常

    异常概念和处理机制 什么是异常? 所谓异常就是指在程序运行的过程中发生的一些不正常事件.(如除0溢出,数组下标越界,所要读取的文件不存在); 异常导致的后果? Java程序的执行过程中如出现异常事件, ...

  10. No Route to Host from master/192.168.2.131 to master:9000 failed on socket t

    host里边添加的ip地址与当前的ip地址(ifconfig可以查看)不一致,修改当前ip地址就可以了.