Mongo 数据库

Mongo 数据库

Mongo数据库是由C++语言编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,其内容储存形式类似json对象,他的字段值可以包含其他文档,数组及文档数据

import pymongo

#连接数据库
#第一种写法
client = pymongo.MongoClient(host = "127.0.0.1",port = 27017)
 #第二种写法
 #client = pymongo.MongoClient('mongodb://127.0.0.1:27017')
 
 #指定使用的数据库
 #使用某个数据库时不需要事先创建数据库,可以直接使用你要用数据库,当插入数据时,系统会自动创建你所用使用的数据库和表(collections)
 #第一种写法
db = client.test 
 #第二种写法
#db = client['test']

 #指定集合
 #第一种写法
collection = db.students
 #第二种写法
#collection = db["students"]

## 插入数据
#单条插入
student = {
     'id' : "151241201",
     'name': "fz",
     'age': 20,
     'gender': 'male'
 }
result = collection.insert_one(student)
print(result)

#多条插入
student1 = {
     'id' : "151241202",
     'name': "hk",
     'age': 23,
     'gender': 'male'
 }

student2 = {
     'id' : "151241203",
     'name': "lina",
     'age': 22,
     'gender': 'female'
 }

student3 = {
     'id' : "151241201",
     'name': "zr",
     'age': 28,
     'gender': 'male'
 }

result = collection.insert_many([student1,student2,student3]) #注意:别漏了中间的方括号,数据将以列表的形式传递
print(result)

## 查询
#可以使用find(),find_one()方法进行查询,其中find_one()查询到是单条数据,而find()则返回一个生成对象
result = collection.find_one({'name':"fz"}) #注意:里面有一个大括号
print(type(result))
print(result)

results = collection.find({'age':20})
print(type(result))
for item in results:
    print(item)

#条件查询
#小于($lt)、大于($gt)、小于等于($lte)、大于等于($gte)、不等于($ne)、在范围内($in)、不再范围内($nin)
#查询年龄大于20的所有学生
results = collection.find({'age':{'$gt':20}})
for item in results:
    print(item)

#还可以进行正则匹配查询
#例如,查询名字以f开头的数据
results = collection.find({'name':{'regex':'^f.*'}})
for item in results:
    print(item)

#功能符号截图

## 计数
#查询表中数据的总数
count = collection.find().count()
print(count)

#要统计查询结果有多少条数据,可以调用count()方法
count = collection.find({'age':{'$gt':20}}).count()
print(count)


## 排序
#排序时,直接调用sort()方法,并在其中传入排序的字段及升降标志即可
results = collection.find().sort("age",pymongo.ASCENDING)
print([result['name'] for result in results])

## 偏移
#可以利用skip()方法偏移几个位置
results = collection.find().sort("age",pymongo.ASCENDING).skip(2)
print([result['name'] for result in results])

#还可以用limit()方法指定要取得结果个数
results = collection.find().sort("age",pymongo.ASCENDING).limit(2)
print([result['name'] for result in results])


## 更新
#对于数据更新,我们可以使用update()方法,指定更新的条件和更新的数据即可
condition = {'name':'lina'}
student = collection.find_one(condition)
student['age'] = 25
result = collection.update_one(condition,{'$set': student})
print(result)
print(result.matched_count,result.modified_count) # matched_count匹配到的条数,modified_count影响的数据条数

#修改多条数据
condition = {'age':{'$gt':20}}
result = collection.update_many(condition,{'$inc':{'age':18}})
print(result.matched_count,result.modified_count)


## 删除
#方法一
#直接调用remove()方法指定删除的条件即可,此时符合条件的所有数据均会被删除
result = collection.remove({'name':'hk'})
print(result)

#方法二
result = collection.delete_one({'name':'zr'})
print(result)
print(result.deleted_count)
result = collection.delete_many({'name':{'$ne':'lina'}})
print(result.deleted_count)

猜你喜欢

转载自blog.csdn.net/fzx1123/article/details/87809757