MongoDB
1 连接MongoDB
使用PyMongo库中的MongoClient。一般情况下传入MongoDB的IP及端口号即可,其中一个参数为host第二个参数为prot,如果不传递参数,端口默认为27017
import pymongo
client = pymongo.MongoClient(host = 'localhost',port=27017)
client = pymongo.MongoClient('mongodb://localhost:27017')
print('OK')
2. 指定数据库
可以建立多个数据库,此处以test为例
#指定方法1:
db = client.test
#指定方法2:
db = client['test']
3. 指定集合
每个数据库包含多个集合,类似于mysql中的表
1.指定集合方法1
collection = db.student
2.指定集合方法2
collection = db['student']
4. 插入数据
students = {'id':'2019001','name':'张','age':20,}
#直接调用collection的insert()方法即可插入数据
result = collection.insert(students)
print(result)#自动分配了一个_id 5d0b339965f257e2dd7fb357
如果插入多条数据,只需要以列表形式存入即可。
5. 查询
可以使用find_one() 和 find()方法进行查询,其中find()返回结果为一个生成器对象
-
find_one()查询单个结果
1.result = collection.find_one({'name':'候赛敏'})
当表中有多个结果的时候,返回的结果也是只有一个
2.find()
result = collection.find({'age':20})
for i in result:
print(i)
| 符号 | 含义 | 示例 |
|---|---|---|
| $lt | 小于 | {'age':{'$lt':20}} |
| $gt | 大于 | {'age':{'$gt':20}} |
| $lte | 小于等于 | |
| $gte | 大于等于 | |
| $ne | 不等 | |
| $in | 在范围内(范围相当于集合不是20~24) | {‘age’:{$in:[20,24]}} |
| $nin | 不在范围内 |
1.print("查询年龄为20的学生信息")
result = collection.find({'age':20})
2.print('查询年龄小于21岁的学生信息')
result = collection.find({'age':{'$lt':21}})
3.print('查询年龄大于21岁的学生信息')
result = collection.find({'age':{'$gt':21}})
4.print('查询年龄小于等于21岁的学生信息')
result = collection.find({'age':{'$lte':21}})
5.print('查询年龄大于等于21岁的学生信息')
result = collection.find({'age':{'$gte':21}})
5.print('查询年龄不等于21岁的学生信息')
result = collection.find({'age':{'$ne':21}})
#这里的结果是20 和 22 而不是20 21 22
6.print('查询年龄在20-22岁之间的学生信息')
result = collection.find({'age':{'$in':[20,22]}})
7.print('查询年龄不在20-21岁之间学生信息')
result = collection.find({'age':{'$nin':[20,21]}}
-
使用正则表达式查询
| 符号 | 含义 | 示例 | |
|---|---|---|---|
| $exists | 判断属性是否存在 | {'name':{'$exists':'True'}} | name属性是否存在 |
| $type | 判断类型 | {‘age’:{'$type':"int"}} | age是否为整型 |
| $mod | 数字模操作 | {'age':{'$mod':[5,0]}} | 年龄模5余0 |
| $where | 高级条件查询 | {'$where':'obj.age==obj.score'} | 年龄等于成绩 |
'''查询某属性值存在'''
result = collection.find({'id':{'$exists':'True'}})
'''判断类型'''
result = collection.find({'age':{'$type':'int'}})
'''对年龄模5余2操作'''
result = collection.find({'age':{'$mod':[5,2]}})
'''高级条件查询'''
result = collection.find({'$where':'obj.age==obj.score'})
6. 计数
统计调用find()方法查询到结果的个数
格式:count = collection.find().count()
7. 顺序
调用sort()方法,并在其中传入排序字段用来控制降序或升序
#升序pymongo.ASCENDING 为升序
result = collection.find().sort('age',pymongo.ASCENDING)
#降序 pymongo.DESCENDING 降序
result1 = collection.find().sort('age',pymongo.DESCENDING)
8. 偏移
如果只想取某几个元素,此时使用skip方法偏移几个位置,如偏移2,忽略前面2个从第三个开始获取元素
result = collection.find().sort('age',pymongo.DESCENDING).skip(2)
也可以用limit()方法指定获取结果的个数
result = collection.find().sort('age',pymongo.DESCENDING).limit(2)
for i in result:
print(i)
9. 更新
使用update()方法,指定更新的条件和更新的数据即可
condition={'name':'候赛敏'}
student = collection.find_one(condition)
student['age']=25
result = collection.update(condition,{'$set':student})
#官方推荐使用update_one()和update_many(),第二个参数需要用$类型操作符作为字典的键,进行更改(要求比较严格)
先指定查询条件,然后查询数据,修改调用updata()方法将原条件修改后传入
返回结果为字典形式, ok代表成功,nModified代表形象的数据条数
result = collection.update(condition,{'$set':student})
10. 删除
直接使用remove()方法,指定删除条件
result = collection.remove({'age':{'$gt':25}})
deleted_one() deleted_many()
浙公网安备 33010602011771号