- 1 import pymongo
- 2
- 3 #连接数据库实例(连接数据库)---》获取相应数据库---》获取相应collection集合(表)
- 4 client = pymongo.MongoClient(host='localhost',port=27017)
- 5
- 6 db = client.test #也可用字典形式操作,如下
- 7 # db = client["test"]
- 8
- 9 collection = db.students #也可用字典形式操作,如下
- 10 # collection = db["students"]
- 11
- 12 student1 = {
- 13 'id':'001',
- 14 'name':'haha',
- 15 'age':20,
- 16 'gender':'male'
- 17 }
- 18 student2 = {
- 19 'id': '002',
- 20 'name': 'Mike',
- 21 'age': 41,
- 22 'gender': 'male'
- 23 }
- 24 #--------------------------------------------------------------------------
- 25 #插入 insert into students(...) values('002',...)
- 26 #若不指定 _id 字段,系统默认会生成一个ObjectId
- 27 #可插入一条或多条数据(列表形式),python3不推荐使用insert
- 28 # collection.insert([student1,student2])
- 29 # collection.insert(student1)
- 30
- 31 #官方推荐,分开使用,返回值不是ObjectId,而是InsertOneResult对象,我们可以调用其inserted_id属性获取_id。
- 32 # result = collection.insert_one(student2)
- 33 # print(result)
- 34 # print(result.inserted_id)
- 35
- 36 # result = collection.insert_many([student1,student2])
- 37 # print(result)
- 38 # print(result.inserted_ids)
- 39
- 40 #------------------------------------------------------------------
- 41 #查询 select * from students where id=002
- 42 #查询条件使用字典,可使用多字段,find是多条查询
- 43 # result_find = collection.find({"name":"lijingbo","age":20})
- 44 # print(result_find.next()) #返回一个游标,游标相当于迭代器,可使用next()获取一条结果,或者使用循环遍历等,遍历结果是字典
- 45 #find_one:单个查询,返回字典类型
- 46 # result = collection.find_one({'age':20})
- 47 # print(result,type(result))
- 48 #结合关系符进行查询:$gt,$lt,$gte,$lte,$ne,$in,$nin
- 49 # result = collection.find({'age':{'$gt':18}})
- 50 # result = collection.find({'age':{'$in':[18,41]}})
- 51 #结合特殊符号查询:$regex
- 52 # result = collection.find({'name':{'$regex':'^M.*'}}) #正则
- 53 # result = collection.find({'name':{'$exists':True}}) #查询含有name属性的
- 54 # result = collection.find({'age':{'$mod':[5,0]}}) #求模,对5取余=0
- 55 # result = collection.find({'$where':'obj.age==20'}) #查询age为20的,obj是自身
- 56 # result = collection.find({'age':20}).count() #统计
- 57 # result = collection.find().sort('age',pymongo.ASCENDING) #按照指定字段升序排列
- 58 # result = collection.find().sort('age',pymongo.DESCENDING) #按照指定字段升序排列
- 59 # result = collection.find().sort('age',pymongo.DESCENDING).skip(2) #按照指定字段升序排列,偏移2个(就是把最前面两个跳过去了)
- 60 # result = collection.find().sort('age',pymongo.DESCENDING).skip(2).limit(5) #限制得到5
- 61 # print(result)
- 62 # for r in result:
- 63 # print(r['name'],r['age'])
- 64
- 65 #----------------------------------------------------------
- 66 #更新 update students set name=haha where id=001
- 67 #参数1:查询条件(字典);参数2:更新值(字典,键:'$set',值:字典【也可直接使用外部字典】)
- 68 #其他:upsert默认为False,为True时——若更新的原数据不存在,则插入数据
- 69 #multi——默认为False只更新查询到的第一条数据,为True时:更新全部查询到的数据
- 70 # $set:是mongodb内置函数,覆盖原始数据
- 71 # collection.update({"id":"001"},{'$set':{'age':34}},upsert=True,multi=True)
- 72 # print(collection.find().next())
- 73 #上面的官方也不推荐,可以使用下面的
- 74 # result = collection.update_one({'name':'haha'},{'$set':{'age':18}})
- 75 # result = collection.update_many({'name':'haha'},{'$set':{'age':18}})
- 76 # print(result) #只修改一条数据,若该数据不修改就和修改条件一样了,那有可能修改数为0
- 77 # print(result.matched_count,result.modified_count)
- 78
- 79
- 80 #-----------------------------------------------------
- 81 #删除,remove方法官方不推荐
- 82 # collection.remove({"id":"001"},justOne=1)
- 83 # result = collection.delete_one({'name':'Mike'})
- 84 # result = collection.delete_many({'name':'Mike'})
- 85 # print(result)
- 86 # print(result.deleted_count)
- 87
- 88 #---------------------------------------------------
- 89 #组合方法
- 90 # result = collection.find_one_and_delete({'name':'haha'})
- 91 # result = collection.find_one_and_update({'name':'haha'},{'$set':{'age':45}})
- 92 # result = collection.find_one_and_replace({'name':'haha'})
- 93 # print(result)