Python连接mongodb一般使用pymongo模块
1. pymongo模块的简单使用
### MongoDB存储 ## 连接MongoDB import pymongo # 建立连接对象,2种方法皆可 client = pymongo.MongoClient(host='10.0.0.100', port=27017) client2 = pymongo.MongoClient('mongodb://10.0.0.100:27017/') # 指定数据库,如无则创建 db = client.test db2 = client2['test'] # 指定集合,如无则创建 collection = db.students collection2 = db2['students'] # 插入数据,字典格式 stu = { 'name': 'dmr', 'age': '25', 'score': '80', 'gender': 'frail' } stu2 = { 'name': 'asx', 'age': '23', 'score': '81', 'gender': 'frail' } stu3 = { 'name': 'scy', 'age': '26', 'score': '66', 'gender': 'male' } # result = collection.insert_one(stu) # result2 = collection.insert_many([stu2, stu3]) # print(result2, result.inserted_id) ''' 输出内容: <pymongo.results.InsertManyResult object at 0x0000000002E9D688> 5e70908ba7233089f696168e ''' # 查询, # find_one()查询到单个结果返回一个字典,如匹配到多个值则返回匹配到的第一个值 # find()则返回一个生成器对象 ## 比较符号 # $lt 小于 {'age': {'$lt': 20}} # $gt 大于 {'age': {'$gt': 20}} # $lte 小于等于 {'age': {'$lte': 20}} # $gte 大于等于 {'age': {'$gte': 20}} # $ne 不等于 {'age': {'$ne': 20}} # $in 范围内 {'age': {'$in': [20, 24]}} # $nin 范围外 {'age': {'$nin': [20, 24]}} ## 功能符号 # $regex 匹配正则表达式 {'name': {'$regex': '^d.*'}} 名字以d开头的 # $exists 属性是否存在 {'name': {'$exists': True}} name属性存在则返回存在的内容 # $type 类型判断 {'age': {'$type': 'int'}} age的类型是否为int,匹配则返回匹配的内容 # $text 匹配正则表达式 {'$text': {'$search': 'dm'}} text类型的属性中包含dm字符串 result = collection.find_one({'name': 'dmr'}) result2 = collection.find({'name': 'dmr'}) print(type(result), type(result2)) print(result, result2) result = collection.find_one({'gender': 'frail'}) result2 = collection.find({'gender': 'frail'}) print(type(result), type(result2)) print(result, result2) for item in result2: print(item) ''' 输出内容: <class 'dict'> <class 'pymongo.cursor.Cursor'> {'_id': ObjectId('5e708dd5b7d59968e4f1ffef'), 'name': 'dmr', 'age': '25', 'score': '80'} <pymongo.cursor.Cursor object at 0x0000000002E9B7B8> <class 'dict'> <class 'pymongo.cursor.Cursor'> {'_id': ObjectId('5e709140e0fe6500676f15c3'), 'name': 'dmr', 'age': '25', 'score': '80', 'gender': 'frail'} <pymongo.cursor.Cursor object at 0x0000000002E9BA90> {'_id': ObjectId('5e709140e0fe6500676f15c3'), 'name': 'dmr', 'age': '25', 'score': '80', 'gender': 'frail'} {'_id': ObjectId('5e709140e0fe6500676f15c4'), 'name': 'asx', 'age': '23', 'score': '81', 'gender': 'frail'} ''' ## 计数 count = collection.find({'gender': {'$exists': True}}).count() print(count) ## 排序 results = collection.find().sort('_id', pymongo.ASCENDING) print(results) print([i for i in results]) ## 偏移,skip(2)及忽略前2个元素,limit(2)选择前2个元素,后面的元素忽略 results = collection.find().sort('_id', pymongo.ASCENDING).skip(2) print(results) print([i for i in results]) ## 更新 old = { 'name': 'dmr' } new = { 'age': 20 } # 直接替换整条内容 result = collection.update(old, new) result2 = collection.update_one(old, {'$set': new}) # 仅修改new中键值的内容 result3 = collection.update(old, {'$set': new}) result4 = collection.update_many(old, {'$set': new}) print(result, result2) r2 = collection.find() for i in r2: print(i) ## 删除,remove,delete_one,delete_many result = collection.remove({'name': 'dmr'}) print(result) result2 = collection.delete_one({'name': 'dmr'}) print(result2.deleted_count) result3 = collection.delete_many({'name': 'scy'}) print(result3.deleted_count) result4 = collection.find() for i in result4: print(i)