您好,欢迎光临本网站![请登录][注册会员]  
文件名称: MongoDB基本操作之Python篇
  所属分类: MongoDB
  开发工具:
  文件大小: 372kb
  下载次数: 0
  上传时间: 2019-07-02
  提 供 者: aba****
 详细说明:MongoDB基本操作之Python篇 连接数据库 MongoClient VS Connection5.2查询特定键(£ ields) select name, age from users where age=21 for u in db. users find((age: 21], ["name",age!): print u for u in db. users find(fields =L"name",age ): printl 5.3排序(SORT) pymmongo. ASCENDING#也可以用1来代替 pymmongc. DESCENDING#也可以用-1来代替 for u in dh. users find(). sort([(age, pynmongo ASCENDING)]): printl# select*from集合名 order by键1 for u in db. users find(). sort([("age", pymongo DESCENDING)]): print u se1ect*from集合名 order by键1desc for u in db. users find(). sort(L("te1", pymongo ASCENDTNG ),("5222 p mongo. DESCENDING)]): print u# select*frcm集合名 order by键1asc,键2desc for u in db. users find(sort =[("1", pymongo ASCENDING),("#2 p mongo, DESCENDING)]): print u#sort的另一种写法 for u in db. users find(t"name": user9" sort=[[ name,l],['sex' 1]], fields =["nane","age",'sex']): print u#组合写法 5.4从第几行开始读取(SLCE),读取多少行( LIMIT) #se1ec*fron集合名skip21imit3 #My的写法:seec*fron集合名imi2,3 for u in db. users find(). skip(2)limit(3): print u for u in db. users find(skip = 2, limit =3): print u #可以用切片代替 skip limit( mongo中的s1ice貌似有点问题) for u in db. users find()[2: 5]: print #单独的写 for u in db. users find(). skip(2): print u for u in db.users find(skip=1): print. u for u in db. users find().Limit(5): print u for u in db. users find(limit =3):print u 5.5多条件查询( Concitiona l operators)#1ike的可使用正则表达式查询 井 selec rom users where name = user3 and age 12 and age 15 for u in db. users find(f'age':[Sgt: 12, '5lt': 151,'name: ' user3) print u select from users where name ='userl and age =21 for u in db. users find(i"age:21,name: user)): print u N for u in db. users find(i"age":("sin:(23,26, 32):1): print u t select from users where age in(23,26, 32) for u in db. users find(i"age":t"Snin":(23,26, 32)1): print u t select from users where age not in (23, 26, 32 5.7统计总数( COUNT) print(cb users. count()) select count(*) from users print(cb users find([age":["Sgt: 30):). count())# select count(*) from users where age 30 8 OR for u in db. users find(i"Sor:Li"age: 251, age: 2811): print u select from集合名 where键=值1ox键1=值2 for u in db. users find( [{"age"::"$1e":23};{"ace":{"sqte":33}}]}) print u# select x from集合名 where键1<=值1or键1>=值2 6.是否存在( exists) db. users find(:Sex: 'Sexists: True):)# select from A where exists 键 db. users find(:' sex':('Sexists': False::)t select from eg where not eXIstS 键 7.正则表达式查询 for u in db. users find(i"name":"Regex": r"(?i)user[135]"ih,["name"]): print u#查询出name为uerl,user3,user5的 8.多级路径的元素值匹配 Documen采取JsoN-1ike这种层级结构,因此我们可以直接用嵌入(mioeα)代替传统关系型数 据库的关联引用( Reference) MongoDB支持以"."分割的 namespace路径,条件表达式中的多级路径须用引号 #如果键里面包含数组,只需简单匹配数组属性是否包含该元素即可查询出来 db.集合名. find one({" address':" address1"}) address是个数组,匹配时仅需包含有即 #查询结果如:{"id": Object.Ia("4c47985089+9b53474170a"),"name":"user1", address:[address"address2 #条件表达式中的多级路径须用引号,以"."分割 u=dh.集合名, find one("im,qq":12345678}) #查询结果如:{"ia": ObjectIe("4c479885089df9b53474170a" / name user :["msn":userlghotmail com",ag: 12345678] print u['im]msn']#显示:user- hotmail1.cor #多级路径的更新 db.集合名. update({"im.qq":12345678:,('sset':{"im,c":12345}}) #查询包含特定键的 for u in db. users find(i"im qa": 'Sexists: True]],[im qa":1:): print u #显示如:("id": Objectid("4c479885089df9b53474170a"),"im":("gq": 12345:} for u in ab users find(i 'data: aoc)): print 5in: id": Objectld("4c47a481b48cde79c6780df5"),"name":users dat. 10};3,"ah for u in db. users find(i'data': ' Seler Match:('a: 1 'b:[Sgt: 511): print u #显示如:("id": ObjectId("4c47a481b48ce79c6780df5"),"name":"user8 I' data: L abc {data:"abc")仅简单匹配数组属性是否包含该元素。 Selemmatch则可以处理更复杂的元素査找 条件。当然也可以写成如下方式: db,集合名.find({"data,a":1,"data.b":{"'sgt':5}) 对数组,还可以直接使用序号进行操作: db集合名.fnd({"data.":3})#序号从开始 #如集合的一列内容 classifyid":"tes=l keyword":[ "name":'test1',#将修改此值为tesτ5(数组下标从开始,下标也是用点) frequence: 21 {"name":'test2',#子表的查询,会匹配到此值 ence:50 子表的修改(子表的其它内容不变) db,集合名, update({" classified":"test1"},{"sset":{"κ eyword.0,name":'test5}}) #子表的查询 db.集合名.fna({" class::toid":"test1","< yord.0.name":"test2"}) 操作 (1)sa11:判断数组属性是否包含全部条件。 db. users insert('name:use3",data:L1,23,4,5,617) db. users insert((name: user4",data:[1,2,3]) for u in db. users find((' data':[Sall:[2,3, 4]])): print u hi: id": Objectld("4c47a133b48cde79c6780cf0"),"name":"user3 [1,2,3,4;5,6,7]} 注意和$in的区别。$in是检查目标属性值是条件表达式中的一员,而$a1则要求属性值包含 全部条件元素。 (2) Ssize:匹配数组属性元素数量。 for u in db. users find('data'Ssize: 3)1): print u #只显示匹配此数组数量的:("id": ObjectId("4c47a13b048cde79c6780df1"), I'name":user4""data :[123]] (3) Styre:判断属性类型。 for u in db. users.find({'t':{" style':-}): print u#查询数字类型的 for u in db, users,find({'t':(" style':2:}): print u#查询字符串类型的 类型值: double string: 2 object: 3 array: 4 binary data: 5 ok jec七id:7 boolean: 8 dace: 9 nu11:10 reqular expression: 11 avascript code: 13 symbol: 14 javascript code with scope: 15 32-bit integer: 16 time stamp: 17 64-bit. integer: 18 min key: 255 max key: 127 (4)snot:取反,表示返回条件不成立的文档。 似乎只能跟正则和mod一起使用???? #还不知如何使用 (5) Sunseε:和$set相反,表示移除文档属性。 for u in db. users find('name: user1:): print. u 5IEJoA: " id": ObjectId("4c479885089df9b53474170a"),"name":"userl" de 5, address address1 address 2 db. users update([name: user), Sunset:I addre ess r age:1}}) for u in db. users find(t name :user:): print u #显示如:{"id": ObjectId("4c479885089df9b53474170a"),"name":"user1” 6) Spush:和 s pushA1都是向数组属性添加元素。#好像两者没啥区别 for u in db. users find(t name: user:): print. u 4显示如:{"id": ObjectId("4c479885089df9b53474170a"),"age":15,"name" :user1 I db. users update(['name':"userl"),I'Spush':[' data: 111) for u in db. users find(t name:user:): print 4显示如:("id": bjectId("4c479885089df9b53474170a"),"age":15,"data" :[11;"name":"user1"} db. users update([' name:user1"),t'SpushAll':t'data:L2,3,4,5])) for u in db. users find(t 'name: user:): print u 524: " id": ObjectId("4c479885089df9b53474170a"),"age": 15,"data" name user1 (7) SaddToset:和$push类似,不过仅在该元素不存在时才添加(set表示不重复元素集合) odate(('nlamne ': "use="),1'Sunset': 'data:1)) db. users update([' name: user2"),i'saddToset':i' data: 111) ab users update( name 2,Saddloset': data:1]1) for u in db. users find(t int 4显示:{"id": ObjectId("c479896089df9b53474170b"),"data":[1 nname":user2 db. users update(['name:"user2"),('Spush':[data: 11)) for u in db. users find('name: user2:): print. u 4显示:{"id": ObjectId("4c479896089df9b53474170b"),"data":[1,11, name":user2". 要添加多个元素,使用 Seach。 db. users update(( name:use=2,f SaddToset:data:seach: [1,2,3,4]}}}) for u in db. users find(f'name:user2 1): print u t hi: (u'age': 12, u' id': ObjectId('4c479896089af9053474170b),u'data': 1,1,2,3,4」,name':u"user2"} 4貌似不会自动删除重复 (8) Seach添加多个元素用 db. users update((name:user2"),I'Sunset':i' data: 1)1) db. users update(( name:use=2,t SaddTaset:i data:1)) for u in ab users find(tname: user2 1): print u #显示:{"id": Objectid("4c479896089df9b53474170b"),"data":[11 ni name": user2 db. users update([name:"user2"),i'SaddToset:data:ISeach [1,2,3,4]}}}) for u in db. users find( name: user2): print u hi: fu' age: 12, u' id': ObjectId('4c479896089af9053474170b),u'data': [1,2,3,4],u'name":1"user2} db. users update(['name: "user2"),1'SaddToset:' data:[, 2,3, 4]11) for u in db. users find(('name:user2): print u #显示:{"id:0 pEctiC("4c479896089df9b534741/0b"),"data":1,2,3 4,[1,2,3,4]1"name":"user2" db. users update(['name: user2,'Sunset':['data: 11) db. users update(I' name: user2,t' SaddToset':'data:LT 4」}}) for u in db. users find(i ' name: user2:): print u t E: "id": ObjectId ("4c47a133148cde79c6780df0"),"data":[I 4]] name user (9)spop:移除数组属性的元素按数组下标移除),spu-1按值移除,Spu11411移除所有符合提 交的元素 db. users update((nane:user2"1,iSunset':[data: 1)) db. users update(['name': "user2"1,I'saddToset':'data:I'Seach':[1, 2, 3, 4 5,6,7,2,3]):) for u in db. users find('name: user2:): print. u hie: I " id": ObjectId("4c47a133148cde7 9c6780cf0")," 7,2,3,"name" use-2〃 db. users. update(name':"usex2"},{"spop':{data':1})#移除最后一个元素 for u in db. users find(t name: user2 :): print u #显示:{"id": ObjectId("4c47a133k48ce79c6780f0"),"ata":[1,2,3 4,5,6;7,2],"name":"user2"} db. users. update({"name':"use2"},{'spop":{'data':-1}})#移除第一个元素 for u in db. users find(I'name: user2";): print u i :I "id": ObjectId ("4c47a133148cde79c6780cf0"),"d 5,6,7,2],"nane":"user2"} db. users. update({'name':"usex2"},{"Spu1l:{'data':2}})#移除全部2 for u in ab users find(tname: user2 1): print u #显示:{"id": ObjectId("4c47a133b48cde79c6780f0"),"aata":[3,4,5, :"user2 db.users.update(("name':"usex2"},{'$pu11A1l:{"data':[3,5,6]}})4移除3,5,6 for u in db. users find(i'nane: user2 1): print u SiE. id": ObjectId(4c17a-33b418cde79c67800f0"),data:[ 4, 7 I name": user2. (10)$ where:用Js代码来代替有些丑陋的$1t、$qt。 MongoDB内置了 Javascript Engine( SpiderMonkey)。可直接使用 Us Expression,甚至 使用 JS Func=ion写更复杂的 Code block。 db. users. remove()4删除集合里的所有记录 for i in range(1 ab users insert(f'name: user+ str(i),'age:i) for u in db. users find(): print u 显示如下: i id: ObjectId(4c47b3372a9b20e866da226e"),name":user0",age": i "id":Objectld("Ac47b3372a9b2be8 66da226f"),name":userl","age:1 ObjectId(4c47b3372a9b2De866da2270),"name":user2 ade i id": ObjectId("4c47b3372a9b20e866da2271"),"name":user3","age jectId(4C47b3372a9b2be866da2272"),"name er4",age ObjectId( 4c47b3372a9b2De866da2273 ),name":user5 i id":ObjectId(4c47b3372a9b2be866da2274),"name":user6" ,"age i id": ObjectId(4c4753372a9b2De866da2275),"name":user"age: I "id": ObjectId(4c47b3372a9b2be866da2276),name":users", age i id": ObjectId("4c47b3372a9b2De866da2277 )"name":user9""age": 9 for u in db. users find(i "Swhere: this age 7 I this age <31): print #显示如下: u ag ctId(4c47b3372a9b2e866da226e") users lu'age: 1.0, u id: ObjectId(4c47b3372a9b202866da226f'),u'name t u user fu' age:2.0,u ic: ObjectId(4c4753372a9b2be866da2270),uname: u'user2 fu age:8.0,u id: ObjectId(4c47b3372a9b20e866da2276),u' name: u users 9.0 a: ObjectId( 4c47b3372a9b2be866da2277),uname u'user9 '1 for u in db. users find() where("this age >7 this age < 3): print u #显示如下: fudge id: ObjectId(4c47b3372a9b2be866da226e'),u'name usero' lu' age: 1.0, u ia: ObjectId (4c47b3372a9b2o2866da226f),u'namc' u user1 du'age:2.0, u id: ObjectId(4c4753372a 9b2De866da2270),uname: user 2 fu age:8.0,u id: ObjectId( 4c17b3372a9b202866da2276),u'name: users {uage":9.0 :ObjectId(4c47b3372a9b2be866da2277),uname: u'userg'
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 输入关键字,在本站1000多万海量源码库中尽情搜索: