首先安装:python
pip install pymongo
安装好了使用mongodb
import pymongo # 连接mongodb,获得一个mongoclient的客户端对象 client = pymongo.MongoClient() # 指定数据库 db = client.test db = client["test"] # 这两种方式均可以指定数据库,若是没有该数据库的话,会自行建立 # 若是了解面向对象的一些魔法(内置)方法的话,大概可以知道client对应的类,确定重写__getattr__,和__getitem__方法 # 指定集合 collection = db.users collection = db["users"] # 一样这两种方法均可以指定到集合,不存在会自行建立
知道了数据库和集合,下面就是对文档的操做了数据库
# 接着上面的内容 # 插入文档 result = collection.insert({"name":"zhuchunyu","age":22}) # 返回值就是一个对象,输出效果为ObjectId('5cde10c2e200928c8fa29315') result = collection.insert([{"name":"zhanghao","age":22},{"name":"dsb","age":22}]) # 上面这行代码是插入多个文档,返回值就是一个list,里面元素就是一个一个的对象 # insert这个方法能够插入多条文档,也能够插入单条文档 # 插入单条文档 result = collection.insert_one({"name":"zhuchunyu","age":22}) result.inserted_id # 返回值也是一个对象,可是这个对象和上面返回值是不同的,本身能够type(result)看看 # 插入多条文档 results = collection.insert_many([{"name":"wuyang","age":21},{"name":"yangjingpeng","age":22}]) # 返回值也是一个对象 # 查询文档 # 大概就是两个方法,find(),find_one() # find()方法,返回值为一个对象 result = collection.find({}) # 将集合里的全部文档都查询出来 result = collection.find({"name":"zhuyu"}) # 查询符合参数一的条件的文档 # 能够经过for循环将文档依次打印出来 for i in result: print(i) # find_one(),查询一条文档,返回值就是一个字典,里面就是文档内容 result = collection.find_one({}) # 只返回符合条件的一个文档数据 # 其实它最终仍是调用的是find()方法,经过limit拿到一条文档数据 # 这两个方法大概能知道作什么事了,下面继续看方法里的参数,此次是重点 # 无论是find_one()仍是find(),他们最终执行的就是dind()这个方法,咱们看这个方法的参数就好了 # find()这个方法,最终返回的就是Cursor这个类的对象,最好是先本身看看源码,咱们继续看这个类 # 咱们主要看这两个参数 filter,和projection # filter就是咱们的查询条件,projection就是指定返回文档的哪些字段数据 # 有这样格式文档数据的集合,文档不止下面这一条,有不少 { _id: 4, name: "xi", age: 34, type: 2, status: "D", favorites: { artist: "Chagall", food: "chocolate" }, finished: [ 5, 11 ], badges: [ "red", "black" ], points: [ { points: 53, bonus: 15 }, { points: 51, bonus: 15 } ] } # 当前这个集合的名字赋值给了collection这个变量 # 查询出name字段为"xi"的全部文档 res = collection.find({"name":"xi"}) # 查询age字段大于20的全部文档数据 res = collection.find({"age":{"$gt":20}}) # 查询status字段为"D",且age字段小于50的全部文档 res = collection.find({"status":"D","age":{"$lt":50}}) # 查询status字段为"D",或者age字段小于50的全部文档 res = collection.find({"$or":[{"status":"D"},{"age":{"$lt":50}}]}) # 查询age字段大于30小于50的文档 res = collection.find({"age": {"$lt": 50, "$gt": 30}}) # 查询finished字段数组有5这个元素的文档 res = collection.find({"finished": 5}) # 查询favorites字段里的文档artist字段为"Chagall"的文档 res = collection.find({"favorites.artist": "Chagall"}) # 查询points字段里的文档字段points为53,而且bonus字段为15的文档 res = collection.find({"points.points": 53, "points.bonus": 15}) # projection就是指定返回文档的哪些字段数据 # 这是第二个参数,传递一个字典,key就是字段名,value就是0或1,0表明不须要,1表明须要 # 好比上面那个例子,我想查询name字段为"xi",且我只须要name,age这两个字段 res = collection.find({"name":"xi"},{"name":1,"age":"1","_id":0}) #注意:find()返回值是Cursor这个类的对象,res能够继续使用该对象里的方法,咱们经过print,或者for循环这个对象,只是触发了它里面的一些内置方法。 # 更新文档 # 大概就是update,update_one,update_many # update(),至少传递两个参数,参数一就是filter(筛选条件),参数二就是更新后的文档 # 好比我有这样的一条文档{"name":"zhuyu","age":22...} # 我想把这条文档的age字段改成23,其余的字段数据不发生变化 res_dict = collection.find_one({"name":"zhuyu"}) res_dict["age"] = 23 collection.update({"name":"zhuyu"},res_dict) # 对了,就算根据筛选条件得出的结果有多条,也只会更新其中的一条文档 # update_one,也是至少传递两个参数,具体的参数能够去看源码,他只会将参数二的给的字段的值进行更新,不会像update那样,整条数据都进行更新 # 仍是继续上面那个例子:将age字段改成23 collection.update_one({"name":"zhuyu"},{"$set":{"age":23}}) # update_many,更新多条文档 # 删除文档,参数至少一个,就是filter(筛选条件) # delete_one() 删除一条文档 # delete_many()删除多条文档