知道了大概的mongodb命令后,再下载Mongodb的python驱动,sudo pip3 install pymongo ,然后在python文件中导入相应的库,进行如下的配置:
1 2 3 4 5 6 7
| from pymongo import MongoClient _client = MongoClient('127.0.0.1', 27017) _db = _client['Mongodb'] _db.authenticate('username', 'password') test = _db['test'] femn = _db['femn']
|
返回结果
insert , insert_one , insert_many
1 2 3 4 5
| insert({'name':1}) insert([{'name':1},{'name':2}]) insert_one insert_many print(r.inserted_ids)
|
update
1
| {'ok': 1, 'updatedExisting': True, 'nModified': 1, 'n': 1}
|
upsert=True
1
| {'ok':1,'nModified':0,'upserted':ObjectId(''),'n':1,'updatedExisting':True}
|
update_one
1 2 3
| r = db.test.update_one({'_id':'xx'},{'$set':{'name':'femn'}}) r.raw_result ={'nModified': 0, 'n': 1, 'updatedExisting': True, 'ok': 1}
|
delete_many delete_one
1 2 3 4
| result = db.test.delete_many({'x': 1}) result.deleted_count result.raw_result
|
find find_one
1 2 3
| find() find({}) find_one({'_id':'xx'}) find_one_and_update()
|
find find_one 命令
1 2 3 4 5 6
| femn.find(filter={'field':'xx'}, projection={'mobile':1,'name':0},limit=10,skip=0, sort=[('update_time':pymongo.DESCENDING)]) femn.find({'name':xx},{'name':1}).limit().skip(). sort=([('update_time':pymongo.DESCENDING)])
|
filter中的多个查询操作符
$all $in $nin 此字段可包含(或排除)多个值的文档
1 2 3 4 5
| ({filed:{'$all':[]}}) ({filed:{'$in','$nin' :['x']}})
|
$regex 正规匹配字段的值
1 2
| ({filed:{'$regex': '%s' % student_name}}) 就是 ' *%s* ' ({filed:{'$not':{'$regex':'femn'}}})
|
$exists 判断文档是否有此字段
1 2
| ({filed: {'$exists': False}})
|
$or 满足一个字段的值就行
1 2
| ({'gender': '女', '$or': [{'expand.expand_time': '2015-01-01'}, {'coin': {'$lt': 2}}]})
|
$slice
1 2
| ({'expand.expand_time':{'$slice':[5,3]}})
|
混合包含和排除不能同时用
1
| ({'album_id':{'$in':[i]}},projection={'play_count':1,'cover_image':1})
|
混合包含和排除不能同时用,既in:[],cover_image:1,comment:0不能同时使用,但可以 in:[],cover_image:1,comment:1
其它的查询操作符 $eachMatch,$gt:2, $lte, $gte, $lt, ‘$slice’ $ne不等于
find_one_and_update命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| find_one_and_update(q,u,p,upsert=False, return_document=ReturnDocument.AFTER) test.update({'name':'femn'}, { '$set':{'name':'xx'}, '$inc':{'age':-1}, '$addToSet':{'name_list':{'$each':['','']}}, '$pull':{'list':{'key':'value'}}, '$pullAll':{'l':[1,2,3]} }) test.update({'_id':id},{'$set': {'recording':{'openid':openid,'book_id':book_id}}}) test.find_one({'_id':bookcase_id,recording.book_id':book_id})
|
改变数值中的值 通过位置或者操作符$
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| test.update({"relationships.fname":"xiong",'_id':id}, {$set:{"relationships.$.age":22}}) test.update({'_id':id}, {'$inc':{'book_class.'+str(i)+'.class_count':count}}) test.update({'_id':id}, {'activity_history':{'click_total':1,per:1,'login':1}}}) test.update({'_id':id}, {'$inc':{'activity_history.0.click_total':1, 'activity_history.0.2017-02':1, 'activity_history.0.login':1}},)
|
注意pull一个个的数组是个整体
1 2 3 4 5 6 7
| 'reservation_list':[{'time':"",'device_id':'', 'order_book':[{1},{2}]},{...},{...}] 'reservation_list':[{'device_id':'','book_id':'', 'book_plu':'','shelf_number':101}]
|
数组字段的内容album_id = ['1','xx'],不是JSON类型
1 2 3 4
| test.update({'_id':id},{'$pull':{'album_id':'xx'}}) {'$pull': {'album_id': {'$in': ['xx']}}
|
删除以字典形式保存的数组
1 2
| {'$pull': {'praise': {'user_id': user_id,'_id':'xx'}} {'$pull': {'save_receiver_address': {'flag': del_flag}}})
|