python操作MongoDB

本文详细的介绍了在python里如何操作MongoDB数据库

在python里操作MongoDB其实很简单主要分三部分

1.连接MongoDB数据库

2.指定数据库

3.指定集合

接下来我们就详细的看一看数据库的连接及插入操作吧:

数据库连接.py

import pymongo

#连接 MongoDB
client=pymongo.MongoClient(host='localhost',port=27017)

#第二种连接方式
#client=pymongo.MongoClient('mongodb://localhost:27017/')

#指定数据库
db=client.test
#第二种指定方式
#db=client['test']

#指定集合
collection=db.students

#collection=db['students']


#插入数据
student={
    'id':'201812121',
    'name':'ltf',
    'age':'21',
    'gender':'male'
}
result=collection.insert(student)
print(result)

student1={
    'id':'201812122',
    'name':'lsq',
    'age':'21',
    'gender':'female'
}
student2={
    'id':'201812123',
    'name':'lhw',
    'age':'20',
    'gender':'male'
}
result1=collection.insert([student1,student2])
print(result1)


student3={
    'id':'201812124',
    'name':'bz',
    'age':'21',
    'gender':'female'
}
student4={
    'id':'201812125',
    'name':'zn',
    'age':'20',
    'gender':'male'
}
result2=collection.insert_many([student3,student4])
print(result2)
print(result2.inserted_ids)


#查询数据
result3=collection.find_one({'name':'ltf'})
print(type(result3))
print(result3)

接下来就是数据库查询操作,主要有find_one和find方法,

import pymongo

#连接 MongoDB
client=pymongo.MongoClient(host='localhost',port=27017)
#指定数据库
db=client.test
#指定集合
collection=db.students

#查询数据
result=collection.find_one({'name':'ltf'})
print(type(result))
print(result)

#查询多条数据
results=collection.find({'age':'20'})
print(results)
for result in results:
    print(result)

print('===='*20)
#查询年龄大于20
resultss=collection.find({'age':{'$gt':'20'}})
for result1 in resultss:
    print(result1)

print('===='*20)
#正则匹配查询
results=collection.find({'name':{'$regex':'^ltf.*'}})
for result in results:
    print(result)
#属性是否存在
results=collection.find({'name':{'$exists':True}})
for result in results:
    print(result)

#文本查询
results=collection.find({'$text':{'$search':'ltf'}})
print(results)

除此之外还有其他各种各样的方法:

import pymongo

#连接 MongoDB
client=pymongo.MongoClient(host='localhost',port=27017)
#指定数据库
db=client.test
#指定集合
collection=db.students

#计数
count=collection.find().count()
print(count)   #统计所有数量

count=collection.find({'age':'20'}).count()
print(count)   #统计年龄为20的数量

#排序
results=collection.find().sort('name',pymongo.ASCENDING)
print([result['name']for result in results])  #升序

results=collection.find().sort('name',pymongo.DESCENDING)
print([result['name']for result in results])  #降序

#偏移
results=collection.find().sort('name',pymongo.ASCENDING).skip(2)
print([result['name']for result in results])  #升序偏移2 即从第三个数据开始输出

results=collection.find().sort('name',pymongo.ASCENDING).skip(2).limit(8)
print([result['name']for result in results])  #升序偏移2 即从第三个数据开始输出 limit限制8个

#更新
condition={'name':'ltf'}
student=collection.find_one(condition)
student['age']='25'
result=collection.update(condition,student)
print(result)

'''
#删除
result=collection.remove({'name':'bz'})
print(result)  #成功一个

result=collection.delete_one({'name':'zn'})
print(result)
print(result.deleted_count) #删除bn

result=collection.delete_many({'age':{'$gt':'20'}})
print(result.deleted_count) #删除bn
'''

以上就是pymongo的各种各样的方法的一些简单用法了

原文地址:https://www.cnblogs.com/yuxuanlian/p/10109245.html