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. JDK11安装后,环境变量的坑

    安装了最新的JDK11,安装完后设置环境变量,打开CMD,没生效 检查了3遍,都没发现问题,在PATH中将JAVA设置移到第一也不行 最后偶然发现,在点击如图右下的‘编辑文本’,用文本方式编辑时,发现 ...

  2. 丑哭了CSDN。

    真是不知道如何设置,忒,,,,不知如何表达.

  3. Memcached命令-存储命令-查找命令-清理命令

    Memcached set 存储命令 Memcached set 命令用于将 value(数据值) 存储在指定的 key(键) 中. 如果set的key已经存在,该命令可以更新该key所对应的原来的数 ...

  4. 计蒜客蓝桥杯模拟赛 后缀字符串:STL_map+贪心

    问题描述 一天蒜头君得到 n 个字符串 si​,每个字符串的长度都不超过 10. 蒜头君在想,在这 n 个字符串中,以 si​ 为后缀的字符串有多少个呢? 输入格式 第一行输入一个整数 n. 接下来  ...

  5. 【机器学习】线性回归sklearn实现

    线性回归原理介绍 线性回归python实现 线性回归sklearn实现 这里使用sklearn框架实现线性回归.使用框架更方便,可以少写很多代码. 写了三个例子,分别是单变量的.双变量的和多变量的.单 ...

  6. Python3 小工具-ARP扫描

    from scapy.all import * import optparse import threading import os def scan(ipt): pkt=Ether(dst='ff: ...

  7. selenium中的三种等待方式(显示等待WebDriverWait()、隐式等待implicitly()、强制等待sleep())---基于python

    我们在实际使用selenium或者appium时,等待下个等待定位的元素出现,特别是web端加载的过程,都需要用到等待,而等待方式的设置是保证脚本稳定有效运行的一个非常重要的手段,在selenium中 ...

  8. 总结Canvas和SVG的区别

    参考链接: 菜鸟教程 HTML5 内联SVG 经典面试题(讨论canvas与svg的区别) Canvas SVG 通过 JavaScript 来绘制 2D 图形 是一种使用 XML 描述 2D 图形的 ...

  9. DDB与DIB

    DB与DIB的区别是什么?觉得书上介绍的有点抽象.不容易理解.他们两者之间的区别的“物理意义” [“现实意义”]——姑且这么叫吧,呵呵!被这个问题困扰了很久,所以今天决定好好查资料总结一下,把它彻底搞 ...

  10. <Effective C++>读书摘要--Designs and Declarations<一>

    <Item 18> Make interfaces easy to use correctly and hard to use incorrectly 1.That being the c ...