1、安装pymongo库在cmd命令提示符中输入 pip3 install pymongo 2、创建数据库的连接import pymongo
coon = pymongo. MongoClient( "localhost" , 27017 )
db = coon. study
my_set = db. student
coon. close( )
3、插入数据函数 说明 insert_one 只能插入一个文档,参数为dict类型 insert_many 插入一个或多个文档,参数为list类型 insert 插入一个或多个文档,参数为dict类型时,插入一个文档,参数为list类型时,插入多个文档 save 只能插入一个文档,参数为dict类型,如果_id域值存在,则进行修改操作
插入一条数据 db.insert({ 键1:值1,键2:值2 }) 插入多条数据 db.insert([ { 键1:值1,键2:值2},{ 键1:值1,键2:值2} ]) my_set. insert( { "stu_name" : "苏芮嘉" ,
"stu_ID" : "2019023127" ,
"stu_age" : 17 ,
"stu_hobby" : [ "舞蹈" , "美术" ] } )
my_set. insert_many( [ { "stu_name" : "张子涛" ,
"stu_ID" : "2019023129" ,
"stu_age" : 16 ,
"stu_sex" : "m" ,
"stu_hobby" : [ "篮球" , "游戏" ] } ,
{ "stu_name" : "刘静怡" ,
"stu_ID" : "2019023130" ,
"stu_age" : 18 ,
"stu_sex" : "w" ,
"stu_hobby" : [ "美术" , "旅游" ] } ,
{ "stu_name" : "郑昕羽" ,
"stu_ID" : "2019023132" ,
"stu_age" : 17 ,
"stu_sex" : "w" ,
"stu_hobby" : [ "跆拳道" , "羽毛球" ] } ] )
4、删除数据函数 说明 remove 删除文档,参数是dict数据类型或_id的值,multi参数默认为True,删除满足条件的全部文档
my_set. remove( { "stu_name" : "郑昕羽" } , multi= False )
my_set. remove( )
5、查找数据函数 说明 find 查找满足条件的全部文档,参数与MongoDB中的写法一样,返回一个游标对象 find_one 查找满足条件的第一个文档,返回一个dict类型的数据
比较操作符和逻辑操作符同mongoshell中的使用方法 函数名 功能 limit() 查询结果显示前几个文档 skip() 跳过前几个文档,显示后面的文档 count() 统计查询到的文档个数 sort() 按照指定的域进行排序,1位升序排列,-1为降序排列
content_list = my_set. find( { "stu_age" : { "$gt" : 17 } } )
for i in content_list:
print ( i[ "stu_name" ] )
for i in content_list. sort( [ ( "stu_name" , 1 ) ] ) :
print ( i)
6、修改数据函数 说明 update 修改文档,参数upsert和multi默认值均为False,用法同mongo shell中的update参数 update_one 修改一个文档 update_many 修改多个文档,相当于update函数中multi参数取值为True
my_set. update( { "stu_ID" : "2019023127" } ,
{ "$set" : { "stu_sex" : "w" } } ,
upsert = True ,
)
my_set. update( { "stu_ID" : "2019023127" } ,
{ "$set" : { "stu_sex" : "w" } } ,
multi = True ,
)
7、索引操作创建索引 ensure_index(),返回索引名称列表 index = my_set. ensure_index( "stu_name" )
print ( index) // [ stu_name_1] 索引名称
index = my_set. ensure_index( [ ( "stu_name" , 1 ) , ( "stu_ID" , - 1 ) ] )
创建多个索引 create_indexes(),返回索引名称列表 index_model_1 = pymongo. IndexModel( [ ( "stu_name" , 1 ) , ( "stu_ID" , - 1 ) ] )
index_model_2 = pymongo. IndexModel( [ ( "stu_age" , 1 ) ] )
indexes = my_set. create_indexes( [ index_model_1, index_model_2] )
print ( indexes)
index = my_set. ensure_index( "stu_ID" , unique= True )
index = my_set. ensure_index( "stu_name" , sparse= True )
查看索引 list_indexes(),返回索引的可迭代对象 for i in my_set. list_indexes( ) :
print ( i)
删除索引drop_index() 和 drop_indexes() my_set. drop_index( "name_1" )
my_set. drop_indexex( )
8、聚合操作函数:aggregate() 功能:实现MongoDB中的聚合操作 参数:也mongo shell 中参数一致 opt_group = { "$group" : { "_id" : "$stu_sex" , "count" : { "$sum" : 1 } } }
for i in my_set. aggregate( [ opt_group] ) :
print ( i)
{ '_id' : 'w' , 'count' : 4 }
{ '_id' : 'm' , 'count' : 2 }
{ '_id' : None , 'count' : 1 }
9、文件操作import pymongo
import gridfs
coon = pymongo. MongoClient( "127.0.0.1" , 27017 )
db = coon. grid
fs = gridfs. GridFS( db)
files = fs. find( )
for file in files:
with open ( file . filename, "wb" ) as f:
while True :
data = file . read( 64 )
if not data:
break
f. write( data)
coon. close( )
import pymongo
import bson. binary
coon = pymongo. MongoClient( "127.0.0.1" , 27017 )
db = coon. file
my_set = db. image
file = "C:\\Users\\Administrator\\Desktop\\3_weixin_43883022.jpg"
f = open ( file , "rb" )
dic = { "content" : bson. binary. Binary( f. read( ) ) , "filename" : "img.jpg" }
my_set. save( dic)
data_list = my_set. find( { "filename" : "img.jpg" } )
for data in data_list:
with open ( "i.jpg" , "wb" ) as f:
f. write( data[ "content" ] )
coon. close( )