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. Python对文本文件逐行扫描,将含有关键字的行存放到另一文件

    #逐行统计关键字行数,并将关键字所在行存放在新的文件中 keyword = "INFO" b = open("C:\\Users\\xxx\\Documents\\new ...

  2. Python3 Tkinter-Button

    1.绑定事件处理函数 from tkinter import * def hello(): print('Hello!') root=Tk() button=Button(root,text='cli ...

  3. Linux 150命令之 文件和目录操作命令 ls

    文件和目录操作命令 ls 查看文件和目录查看显示详信息 ls 工具的参数 ls -l 查看文件详细信息 ls -h 查看文件的大小 ls -ld 只查看目录信息 ls –F 给不同文件加上不同标记 l ...

  4. wpa_supplicant上行接口浅析

    摘自http://blog.csdn.net/fxfzz/article/details/6176414 wpa_supplicant提供的接口 从通信层次上划分, 上行接口:wpa_supplica ...

  5. 如果jsp表单元素的值为空,如何避免null出现在页面上?

    可以写一个简单的函数对空值进行处理,判断值是否为空,如果是空就返回空字符串.实例代码如下: <%! String blanknull(String s) { return (s == null) ...

  6. Linux的ll命令详解

    ll 列出来的结果详细,有时间,是否可读写等信息 ,象windows里的 详细信息 ls 只列出文件名或目录名 就象windows里的 列表 ll -t 是降序,  ll -t | tac 是升序 l ...

  7. iOS开发UIColor,CGColor,CIColor三者的区别和联系

    最近看了看CoreGraphics的东西,看到关于CGColor的东西,于是就想着顺便看看UIColor,CIColor,弄清楚它们之间的区别和联系.下面我们分别看看它们三个的概念: 一.UIColo ...

  8. Crash使用参考

    整理自man 8 crash 1.简介 Crash工具可以用来分析一个正在运行的内核,也可以用来分析一个内核的crash dump文件,这里说的是内核代码异常产生的crash dump文件,不是应用层 ...

  9. <Effective C++>读书摘要--Implementations<一>

    1.For the most part, coming up with appropriate definitions for your classes (and class templates) a ...

  10. 转 【关于api-ms-win-crt-runtimel1-1-0.dll缺失的解决方案】

    关于api-ms-win-crt-runtimel1-1-0.dll缺失的解决方案 目录 关于api-ms-win-crt-runtimel1-1-0dll缺失的解决方案 目录 安装VC redite ...