Python连接MongoDB数据库进行增删改查
"""Mongodb条件查询:
<<比较符号>>
符号: 含义: 示例:
1. $gt 大于 {'age': {'$gt': 20}}
2. $lt 小于 {'age': {'$lt': 20}}
3. $gte 大于等于 {'age': {'$gte': 20}}
4. $lte 小于等于 {'age': {'$lte': 20}}
5. $ne: 不等于 {'age': {'$ne': 20}}
6. $in 在范围内 {'age': {'$in': [20, 30]}}
7. $nin 不在范围内 {'age': {'$nin': [20, 30]}}
<<功能符号>>
符号: 含义: 示例:
1. $regex 匹配正则表达式 {'name': {'$regex': '^M.*'}} # 查询以M开头的name值
2. $exists 属性是否存在 {'name': {'$exists': True}} # 查询name属性是否存在
3. $type 类型判断 {'age': {'$type': 'int'}} # 查询age的类型为int
3. $mod 数字模操作 {'age': {'$mod': [5, 0]]}} # 查询年龄为模5余0的结果
3. $text 文本查询 {'$text': {'$search': 'Mike'}} # 查询text类型的属性中包含'Mike'字符串的结果
3. $where 高级条件查询 {'name': {'$where': 'obj.fans_count == obj.follows_count'}} # 自身粉丝数等于关注数
Mongodb数据更新指令: (指令必须使用双引号)
1: $inc增加字段值
db.test.update({'id':6},{"$inc":{'id':2}}) # result:id=8
db.test.update({'id':6},{$inc:{id:2}}) # 在mongodb交互环境中的写法
2: $set更新字段值
db.test.update({'id':6},{"$set":{'id':2}}) result:id=2
db.test.update({'id':6},{$set:{id:2}}) # 在mongodb交互环境中的写法
3: $unset删除字段
db.test.update({'id':6},{"$unset":{'id':6}})
db.test.update({'id':6},{$unset:{id:6}}) # 在mongodb交互环境中的写法
4: $rename重命名字段
db.test.update({'id':1},{"$rename":{'id':'name'}})
db.test.update({id:10},{$rename:{id:'name'}}) # 在mongodb交互环境中的写法
"""
import pymongo
from bson.objectid import ObjectId
def mongodb_handle(host='localhost', port=27017):
"""
连接mongodb客户端
:param host: mongodb host
:param port: mongodb access port
:return:
"""
client = pymongo.MongoClient(host=host, port=port)
database = client['example']
db_name = eval(str(database).split()[-1][:-1])
print('创建数据库: {}'.format(db_name))
collection = database['sample']
collection_name = eval(str(collection).split()[-1][:-1])
print('创建集合: {}'.format(collection_name))
name = dict(name='Evan')
age = dict(age=20)
stature = dict(stature=177)
collection.insert_one(name)
collection.insert_many([age, stature])
update_format = {"$set": {'stature': 26}}
result = collection.update_one(age, update_format)
print('更新个数: {}'.format(result.matched_count))
collection.update_many(age, update_format)
print(collection.find_one({'_id': ObjectId('5dd642b94957a6800e0187c1')}))
print(collection.find_one(name))
print(collection.find_one({'age': {'$gt': 10}}))
print(collection.find_one({'name': {'$regex': 'Ev.+'}}))
print([i for i in collection.find(name)])
print([i for i in collection.find()])
print('查询name字段个数: {}'.format(collection.count_documents(name)))
print([collection.find().sort('name', pymongo.DESCENDING)])
print([collection.find().sort('name', pymongo.ASCENDING)])
print([collection.find().sort('name', pymongo.ASCENDING).skip(2)])
print([collection.find().sort('name', pymongo.ASCENDING).skip(2).limit(2)])
result = collection.delete_one(stature)
print('删除个数: {}'.format(result.deleted_count))
collection.delete_many(stature)
client.close()
if __name__ == '__main__':
mongodb_handle(host='localhost')