pymongo的基本使用


一、链接数据库

  # 链接数据库se7en521是账号,123456是密码,211.159.185.88是地址,27017是端口号
  client = MongoClient('mongodb://se7en521:123456@211.159.185.88:27017')
  # 指定需要链接的数据库
  mongo_DB = client['video']
  # 指定需要操作的数据库中的表
  video_old = mongo_DB.video_old

二、增

  一、增(插入单条,系统已经不推荐使用)
  result1 = video_old.insert({'vid':'10086','category':"111.1.1_1.1",'type':'3','title':'test'})
  print('result1=%s'%result1)
  print(type(result1))
  # 类型是ObjectID类型,及返回值是_id
  # result1 = 5ee2e5585979c83dd911d1ca
  # <class 'bson.objectid.ObjectId'>

  二、增(插入多条,系统已经不推荐使用)
  result2 = video_old.insert([{'vid':'10087','category':"111.1.1_1.2",'type':'3','title':'test'},{'vid':'10088','category':"111.1.1_1.3",'type':'3','title':'test'}])
  print('result2=%s' % result2)
  print(type(result2))

  # 返回结果是个数组,数组里面包含的插入的多个信息的_id
  # result2 = [ObjectId('5ee2e638c9f38c53aaf5728b'), ObjectId('5ee2e638c9f38c53aaf5728c')]
  # <class 'list'>

  

  三、增(插入单条,系统推荐)
  result3 = video_old.insert_one({'vid':'10089','category':"111.1.1_1.4",'type':'3','title':'test'})
  print('result3=%s'%result3)
  print(type(result3))
  # 要想验证增加成功与否可以使用result3.inserted_id是否有值判断
  print('result3.inserted_id=%s'%result3.inserted_id)
  print(type(result3.inserted_id))

  # result3 = < pymongo.results.InsertOneResult object at0x7fc103890a88 >
  # <class 'pymongo.results.InsertOneResult'>
  # result3.inserted_id = 5ee2e813e39c4604c798d7c4
  # <class 'bson.objectid.ObjectId'>

  四、增(插入多条,系统推荐)
  result4 = video_old.insert_many([{'vid':'10090','category':"111.1.1_1.5",'type':'3','title':'test'},{'vid':'10091','category':"111.1.1_1.6",'type':'3','title':'test'}])
  print('result4=%s'%result4)
  print(type(result4))
  print('result4.inserted_ids=%s'%result4.inserted_ids)
  print(type(result4.inserted_ids))
  
  # result4 = < pymongo.results.InsertManyResult object at 0x7fd32f090c08 >
  # <class 'pymongo.results.InsertManyResult'>
  # result4.inserted_ids = [ObjectId('5ee2e9cc29bd0886c0d6c836'), ObjectId('5ee2e9cc29bd0886c0d6c837')]
  # <class 'list'>

三、查

  五、查询(查询一个,如果有多个满足情况的,返回第一个)
  result5 = video_old.find_one({'type':'3'})
  print('result5=%s'%result5)
  print(type(result5))
  # result5 = {'_id': ObjectId('5ee2e5585979c83dd911d1ca'), 'vid': '10086', 'category': '111.1.1_1.1', 'type': '3','title': 'test'}
  # <class 'dict'>

  六、查询(多个)
  result6=video_old.find({'type':'3'})
  print('result6=%s'%result6)
  print('type=%s'%type(result6))

  # result6 = < pymongo.cursor.Cursor object at 0x7f9ad488f7f0 >
  # type = <class 'pymongo.cursor.Cursor'>
  # 注意一、返回结果是Cursor类型,相当于一个生成器,我们需要遍历取到所有的结果,每一个结果都是字典类型。
  # 注意二、该函数没有find_many(),而是就是find()

  七、统计
  result7 = video_old.find({'type':'3'}).count()
  print('result7=%d'%result7)
  # result7 = 6

  八、排序
  result8 = video_old.find({'type':'3'}).sort('vid',pymongo.DESCENDING) #
  result8_list = [u for u in result8]
  print('result8_list=%s'%result8_list)
  # result8_list = [
  # {'_id': ObjectId('5ee2e9cc29bd0886c0d6c837'), 'vid': '10091', 'category': '111.1.1_1.6', 'type': '3','title': 'test'},
  # {'_id': ObjectId('5ee2e9cc29bd0886c0d6c836'), 'vid': '10090', 'category': '111.1.1_1.5', 'type': '3','title': 'test'},
  # {'_id': ObjectId('5ee2e813e39c4604c798d7c4'), 'vid': '10089', 'category': '111.1.1_1.4', 'type': '3','title': 'test'},
  # {'_id': ObjectId('5ee2e638c9f38c53aaf5728c'), 'vid': '10088', 'category': '111.1.1_1.3', 'type': '3','title': 'test'},
  # {'_id': ObjectId('5ee2e638c9f38c53aaf5728b'), 'vid': '10087', 'category': '111.1.1_1.2', 'type': '3','title': 'test'},
  # {'_id': ObjectId('5ee2e5585979c83dd911d1ca'), 'vid': '10086', 'category': '111.1.1_1.1', 'type': '3','title': 'test'}]

  九、偏移
  # result9 = video_old.find({'type':'3'}).skip(3)
  # result9_list = [u for u in result9]
  # print('result9_list=%s'%result9_list)
  # result9_list = [
  # {'_id': ObjectId('5ee2e813e39c4604c798d7c4'), 'vid': '10089', 'category': '111.1.1_1.4', 'type': '3','title': 'test'},
  # {'_id': ObjectId('5ee2e9cc29bd0886c0d6c836'), 'vid': '10090', 'category': '111.1.1_1.5', 'type': '3','title': 'test'},
  # {'_id': ObjectId('5ee2e9cc29bd0886c0d6c837'), 'vid': '10091', 'category': '111.1.1_1.6', 'type': '3','title': 'test'}]

  十、限制数量
  # result10 = video_old.find({'type':'3'}).limit(3)
  # result10_list = [u for u in result10]
  # print('result10_list=%s'%result10_list)
  # result10_list=[{'_id': ObjectId('5ee2e5585979c83dd911d1ca'), 'vid': '10086', 'category': '111.1.1_1.1', 'type': '3','title': 'test'},
  # {'_id': ObjectId('5ee2e638c9f38c53aaf5728b'), 'vid': '10087', 'category': '111.1.1_1.2', 'type': '3','title': 'test'},
  # {'_id': ObjectId('5ee2e638c9f38c53aaf5728c'), 'vid': '10088', 'category': '111.1.1_1.3', 'type': '3','title': 'test'}]

四、更新

  十一、更新(update)
  result11 = video_old.update({'vid':'10086'},{'$set':{'title':'test1'}})
  result111 = video_old.find_one({'vid':'10086'})
  print('result11=%s'%result11)
  print('result11.get(n)=%d'%result11.get('n'))
  print('result111=%s'%result111)
  print('type=%s'%type(result11))

  1、更新成功
  # result11 = {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}
  # result111 = {'_id': ObjectId('5ee2e5585979c83dd911d1ca'), 'vid': '10086', 'category': '111.1.1_1.1', 'type': '3','title': 'test1'}
  # type = <class 'dict'>

  2、同样成功,但是没有改的的情况
  # result11 = {'n': 1, 'nModified': 0, 'ok': 1.0, 'updatedExisting': True}
  # result111 = {'_id': ObjectId('5ee2e5585979c83dd911d1ca'), 'vid': '10086', 'category': '111.1.1_1.1', 'type': '3','title': 'test1'}
  # type = <class 'dict'>

  3、更新目标不存在,更新失败的情况
  # result11 = {'n': 0, 'nModified': 0, 'ok': 1.0, 'updatedExisting': False}
  # result111 = {'_id': ObjectId('5ee2e5585979c83dd911d1ca'), 'vid': '10086', 'category': '111.1.1_1.1', 'type': '3','title': 'test1'}
  # type = <class 'dict'>

  # 返回结果是字典形式,ok即代表执行成功(因为ok仅代表语句执行成功,不代表更新成功,所以不能用ok判断更新是否成功),
  # nModified代表影响的数据条数(就像第2中情况,同样更新成功,只是更新的内容和原来一样,nModified就为0,所以也不能当做判断条件)。
  # n=1表示成功,n=0表示失败,可以当做判断条件
  # updatedExisting表示更新目标是否存在,就算存在也有可能更新失败,所以也不推荐当做判断条件

  # 十二、更新(update_one)
  result12 = video_old.update_one({'vid':'10087'},{'$set':{'title':'test1'}})
  result121 = video_old.find_one({'vid':'10087'})
  print('result12=%s' % result12)
  print('result12.matched_count=%s'%result12.matched_count)
  print('result12.modified_count=%s'%result12.modified_count)
  print('result12.upserted_id=%s'%result12.upserted_id)
  print('result12.raw_result=%s'%result12.raw_result)
  print('result12.row_result.get(n)=%s'%result12.raw_result.get('n'))
  print('result121=%s' % result121)
  print('type=%s' % type(result12))

  # 情况1、更新数据和原数据不一样,更新成功
  # result12 = < pymongo.results.UpdateResult object at 0x7f9286877b88 >
  # result12.matched_count = 1 可以使用该值判断更新是否成功
  # result12.modified_count = 1
  # result12.upserted_id = None
  # result12.raw_result = {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}
  # result121 = {'_id': ObjectId('5ee2e638c9f38c53aaf5728b'), 'vid': '10087', 'category': '111.1.1_1.2','type': '3', 'title': 'test1'}
  # type = <class 'pymongo.results.UpdateResult'>

  # 情况2
  # result12 = < pymongo.results.UpdateResult object at 0x7f9286877688 >
  # result12.matched_count = 1
  # result12.modified_count = 0
  # result12.upserted_id = None
  # result12.raw_result = {'n': 1, 'nModified': 0, 'ok': 1.0, 'updatedExisting': True}
  # result121 = {'_id': ObjectId('5ee2e638c9f38c53aaf5728b'), 'vid': '10087', 'category': '111.1.1_1.2','type': '3', 'title': 'test1'}
  # type = <class 'pymongo.results.UpdateResult'>

  十三、更新(update_many)
  result13 = video_old.update_many({'title':'test1'},{'$set':{'title':'test2'}})
  print('result13=%s' % result13)
  print('result13.matched_count=%s' % result13.matched_count)
  print('result13.modified_count=%s' % result13.modified_count)
  print('result13.upserted_id=%s' % result13.upserted_id)
  print('result13.raw_result=%s' % result13.raw_result)
  print('result13.row_result.get(n)=%s' % result13.raw_result.get('n'))
  print('type=%s' % type(result13))

  # result13 = < pymongo.results.UpdateResult object at 0x7faaff996ac8 >
  # result13.matched_count = 2 使用改数据不为0判断更新成功
  # result13.modified_count = 2
  # result13.upserted_id = None
  # result13.raw_result = {'n': 2, 'nModified': 2, 'ok': 1.0, 'updatedExisting': True}
  # result13.row_result.get(n) = 2
  # type = <class 'pymongo.results.UpdateResult'>

五、删除

  十四、删除(remove)
  result14 = video_old.remove({'title':'test2'})
  print(result14)
  # {'n': 1, 'ok': 1.0} 可以根据n值不为0判断删除成功
  # {'n': 0, 'ok': 1.0}
  # {'n': 2, 'ok': 1.0}

  十五、删除(delete_one)
  result15 = video_old.delete_one({'vid':'10088'})
  print('result15=%s'%result15)
  print('type=%s'%type(result15))
  print('result15.deleted_count=%d'%result15.deleted_count)
  print('result15.raw_result=%s'%result15.raw_result)

  # result15 = < pymongo.results.DeleteResult object at 0x7fe6af999688 >
  # type = <class 'pymongo.results.DeleteResult'>
  # result15.deleted_count = 1 可以使用该值不为0判断删除是否成功
  # result15.raw_result = {'n': 1, 'ok': 1.0}

  # 十六、删除(delete_many)
  result16 = video_old.delete_many({'title': 'test'})
  print('result16=%s' % result16)
  print('type=%s' % type(result16))
  print('result16.deleted_count=%d' % result16.deleted_count)
  print('result16.raw_result=%s' % result16.raw_result)

  # result16 = < pymongo.results.DeleteResult object at 0x7fc709954f48 >
  # type = <class 'pymongo.results.DeleteResult'>
  # result16.deleted_count = 3 可以使用该值不为0判断删除是否成功
  # result16.raw_result = {'n': 3, 'ok': 1.0}



作者:Se7eN_HOU,发布于:2020/06/30
原文:https://www.cnblogs.com/Se7eN-HOU/p/13208052.html