Python就业班——MongoDB数据库

1、mongodb命令启动与停止服务

>net start "mongodb server"
>net stop "mongodb server"

 

2、命令行客户端

>mongo --host localhost --port 27017
>use school
>show dbs
>db.student.save({name: "Scott", age: 99, sex: "male", city: "Beijing"})
>show dbs

 

3、mongoDB内置角色

以下四个角色的权限仅限于某个逻辑库,不能管理其他逻辑库

Read:允许用户读取指定逻辑库

readWrite:允许用户读写指定逻辑库

dbAdmin:可以管理指定的逻辑库

userAdmin:可以管理指定逻辑库的用户

以下四个角色只能创建在admin逻辑库中,可以管理其他逻辑库

readAnyDatabase:允许读取任何逻辑库

readWriteAnyDatabase:允许读写任何逻辑库

dbAdminAnyDatabase:允许管理任何逻辑库

userAdminAnyDatabase:允许管理任何逻辑库用户

以下两个角色必须创建在admin逻辑库,root角色权限最大

clusterAdmin:允许管理MongoDB集群

root:超级管理员,拥有最高权限

 

4、设置登录帐户

use admin
db.createUser({
    user: "admin",
    pwd: "admin",
    roles: [{role: "root", db: "admin"}),
   mechanisms: ["SCRAM-SHA-1"] 
})

 

5、开启登录验证功能

创建MongoDB配置文件mongo.cnf

dbpath=C:\Program Files\MongoDB\Server\4.0\data
logpath
=C:\Program Files\MongoDB\Server\4.0\logs\mongod.log
auth=true

为了让MongoDB服务加载mongoDB.cnf文件,需要重新安装MongoDB服务(PowerShell执行下面命令)

mongod --config "C:\Program Files\MongoDB\Server\4.0\mongoDB.cnf" --reinstall

 

6、登录MongoDB

use admin
db.auth("admin", "admin")

 

7、操作MongoDB

创建/切换逻辑库:use test

查看逻辑库:show dbs

删除逻辑库:db.dropDatabase()

创建集合:db.createCollection("student")

查看集合:show collections

删除集合:db.student.drop()

查看集合记录数量:db.student.count()

查看数据空间容量:db.student.dataSize()

重命名集合:db.student.renameCollection("stu")

插入数据:

db.student.save([
{name: "AA", age: 18, sex: "male"},
{name: "BB", age: 18, sex: "male"},
{name: "VV", age: 18, sex: "male"},
{name: "CC", age: 18, sex: "male"}
])

时间戳:MongoDB存储日期会自动转换成格林尼治时区

ObjectId("1f5c6ce09080f119f2fd1ce0").getTimestamp()

查询记录:

db.student.find()
db.student.find({name: "AA", sex: "male"})
db.student.find({sex: "male", age: {$gte: 20}})
db.student.findOne()

表达式:

$lt 小于
$gt 大于
$lte 小于等于
$gte 大于等于
$in 包括
$nin 不包括
$ne 不等于
$all 全部
$not 取反
$or 或关系
$exists 含有字段
use school
db.student.find({
    age: {$gte: 20, $lte: 38}
})
// 插入一条数据
db.teacher.save({name: "Jim", role: ["班主任", "年级主任", "校长"]})
//查询身份是班主任和年级主任的教师
db.teacher.find({role: {$all: ["班主任", "年级主任"]}})
// 查询年龄不在28~30之间的学生
db.student.find({age: {$not: {$gte: 18, $lte: 30}}})
// 查询30岁以下的男学生,或者25岁以下的女学生
db.student.find({
    $or: [
        {age: {$lt: 30}, sex: "男"},
        {age: {$lt: 25}, sex: "女"}
    ]
})
// 查询含有age字段的学生
db.student.find({age: {$exists: 1}})

 正则表达式:

db.student.find({name: /^/})
db.student.find({name: /^[a-zA-Z]{2,10}$/})

 分页查询:

db.student.find().limit(20);
db.student.find().skip(20).limit(10);
db.student.find({name:/^[a-zA-Z1-9]{2,10}$/}).skip(1).limit(2)

数据排序:(1代表升序,-1代表降序)

db.student.find().sort({name: 1})
db.student.find().sort({name:  -1}).skip(10).limit(10)
db.student.find({name:/^[a-zA-Z1-9]{2,10}$/}).sort({name: -1}).skip(1).limit(2)

排除重复:distinct()函数代替find()函数查找不重复的记录

db.student.distinct("name")
db.student.distinct("name").sort(function() { return -1 })
db.student.distinct("name").slice(0, 5)
db.student.distinct("name").sort(()=>{return 1}).slice(0, 2)

删除、修改记录:

update:修改符合条件的第一条记录

updateMany:修改符合条件的所有记录

db.collection.update({condition}, {$set: {data}})
db.collection.updateMany({condition}, {$set: {data}})

// 把李强年龄修改成26岁,班级修改为2-6 db.collection.
update({name: "李强"}, {$set: {age: 26, classno: "2-6"}})
// 把26岁以上的男同学班级修改为2-6 db.collection.updateMany({sex: "男", age: {$gte:
25}}, {$set: {classno: "2-6"}})

$unset:删除记录中的字段

$inc:对某个字段值都做加法运算

db.student.updateMany({}, {$unset: {city: 1, tel: 1}})

db.student.updateMany({},  {$inc: {age:2}})

$push:向数组属性添加元素

$pull:删除数组属性元素

db.teacher.update({name: "Jim"}, {$push: {role: "教务主任"}})
db.teacher.update({name: "Jim"}, {$pull: {role: "副校长"}})

remove():删除记录

db.student.remove({})
db.student.remove({class: "6-2", sex: "男"})

创建索引:

db.collection.createIndex({key: 1}, options)

// 1升序,-1降序 db.student.createIndex({name:
1}) // 删除指定索引 db.student.dropIndex("index_name") // 删除所有索引 db.student.dropIndexes() // 创建索引会阻塞MongoDB,影响增删改查操作,在参数添加background,让索引在空闲的时候创建;name设置索引名; db.student.createIndex({name: 1}, {background: true, name: "name"}) // 获取所有索引 db.student.getIndexes()

唯一性索引:只能创建在每个记录都含有的公共字段上,在非公共字段上是不能创建唯一性索引的

// 创建唯一性索引
db.student.createIndex({sid: 1}, {background: true, unique: true, name: "sid_index"})

 创建索引的原则:

_1.数据量很大的集合必须创建索引,相反则不需要创建索引

_2.集合的数据读取过多写入,则需要创建索引

_3.给经常被当做查询条件的字段设置索引

————————————————————————————

use school
// db.student.save({name:"jack", age: 19})
// db.dropDatabase()
// db.createCollection("teacher")
// show collections
// db.teacher.drop()
// db.student.count()
// db.student.dataSize()
// db.student.renameCollection("stu")
// show collections
// db.student.save([
// {name: "AA", age: 18, sex: "male"},
// {name: "BB", age: 18, sex: "male"},
// {name: "VV", age: 18, sex: "male"},
// {name: "CC", age: 18, sex: "male"}
// ])
// ObjectId("1f5c6ce09080f119f2fd1ce0").getTimestamp()
// db.student.find()
// db.student.find({name: "BB", sex: "male"})
// db.student.find({sex: "male", age: {$gte: 20}})

/*
db.student.find({
    age: {$gte: 20, $lte: 38}
})
// 插入一条数据
db.teacher.save({name: "Jim", role: ["班主任", "年级主任", "校长"]})
//查询身份是班主任和年级主任的教师
db.teacher.find({role: {$all: ["班主任", "年级主任"]}})
// 查询年龄不在28~30之间的学生
db.student.find({age: {$not: {$gte: 18, $lte: 30}}})
// 查询30岁以下的男学生,或者25岁以下的女学生
db.student.find({
    $or: [
        {age: {$lt: 30}, sex: "男"},
        {age: {$lt: 25}, sex: "女"}
    ]
})
// 查询含有age字段的学生
db.student.find({age: {$exists: 1}})
*/
// db.student.find().limit(2)
// db.student.find().skip(1).limit(3)

// db.student.distinct("sex")
// db.student.distinct("sex").sort(()=>{return -1})
// db.student.distinct("sex").slice(0, 1)

8、数据导出导入

_1.mongoexport导出集合数据(csv、json)

mongoexport --host=localhost --port=27017 -u admin -p admin --authenticationDatabase=admin -d school -c student [-f "_id,name,sex,age"] -o D:/student.json

_2.mongoimport导入集合的数据

mongoimport --host=localhost --port=27017 -u admin -p admin --authenticationDatabase=admin -d test -c student --file=D:/student.json

_3.mongodump导出逻辑库的数据

--dumpDbUsersAndRoles参数可以备份隶属于逻辑库的用户

mongodump --host=localhost --port=27017 -u admin -p admin --authenticationDatabase=admin [-d school] -o D:/

_4.mongorestore导入逻辑库数据

--drop删除已有的数据在导入

mongorestore --host=localhost --port=27017 -u admin -p admin -authenticationDatabase=admin [--drop] -d school D:/school

 9、安装pymongo

pip install pymongo

10、创建连接、基本操作

MongoClient客户端代理对象用来执行增删改查操作,内置连接池

from pymongo import MongoClient

client = MongoClient(host="localhost", port=27017)
client.admin.authenticate("admin", "admin")

_1. 数据写入:insert_one、insert_many

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
# Project:pythonMongoDBDemo
# File:1、数据写入.py
# Data:2020/10/18 14:37
# Author:LGSP_Harold
from mongo_db import client

client.school.teacher.insert_one({"name": "Jim"})
client.school.teacher.insert_many([
    {"name": "Lucifer"},
    {"name": "Harold"},
    {"name": "Adolph"},
    {"name": "Leslie"}
])


if __name__ == '__main__':
    pass

_2.数据查询:find_one、find

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
# Project:pythonMongoDBDemo
# File:2、数据查询.py
# Data:2020/10/18 17:13
# Author:LGSP_Harold
from mongo_db import client

try:
    teachers = client.school.teacher.find({})
    for one in teachers:
        print(one["_id"], one["name"])
    print("________________")

    teacher = client.school.teacher.find_one({"name": "Jim", "role": "校长"})
    print(teacher["_id"], teacher["name"], teacher["role"])
except Exception as e:
    print(e)


if __name__ == '__main__':
    pass

_3.数据修改:update_one、update_many

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
# Project:pythonMongoDBDemo
# File:3、数据修改.py
# Data:2020/11/9 15:09
# Author:LGSP_Harold
from mongo_db import client

try:
    client.school.teacher.update_many({}, {"$set": {"role": ["班主任"]}})
    client.school.teacher.update_one({"name": "Harold"}, {"$set": {"sex": "", "age": 95}})
    client.school.teacher.update_one({"name": "Harold", "sex": {"$ne": ""}}, {"$push": {"role": "校长"}})
except Exception as e:
    print(e)


if __name__ == '__main__':
    pass

_4.数据删除:delete_one、delete_many

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
# Project:pythonMongoDBDemo
# File:4、数据删除.py
# Data:2020/11/9 17:53
# Author:LGSP_Harold
from mongo_db import client

try:
    client.school.teacher.delete_one({"name": "Jim"})
    client.school.teacher.delete_many({})
except Exception as e:
    print(e)

if __name__ == '__main__':
    pass

_5.其他操作:skip、limit、count、distinct、sort

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
# Project:pythonMongoDBDemo
# File:5、其他操作.py
# Data:2020/11/9 18:02
# Author:LGSP_Harold
from mongo_db import client

try:
    students = client.school.student.find({}).skip(0).limit(10)
    for one in students:
        print(one)

    result = client.school.student.distinct("age")
    for one in result:
        print(one)

    students = client.school.student.find({}).sort([("name", -1)])
    for one in students:
        print(one)

    count = client.school.student.find({}).count()
    print(count)
except Exception as e:
    print(e)


if __name__ == '__main__':
    pass

 11、把文件存放在NoSql数据库:

_1.MongoDB默认存储16M,可在配置文件修改(不建议修改,16M是最经济的存储方案)

_2.GridFS存储引擎,主要用于存储超过16M的文件,对大文件有着更好的性能

Grid FS使用两个集合来存储文件,一个是chunks集合,用来存放文件;另一个集合是files,用于存储文件的元数据(名字、大小、上传时间、自行规定的属性信息等)

Grid FS会把文件分割成若干chunks(256KB),然后在files记录它们

_2.1.默认情况下MongoClient不提供操作GridFS,需要创建GridFS对象

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
# Project:pythonMongoDBDemo
# File:6、GridFS.py
# Data:2020/11/9 22:14
# Author:LGSP_Harold
from mongo_db import client
from gridfs import GridFS

db = client.school
gfs = GridFS(db, "book")


if __name__ == '__main__':
    pass

_2.2.保存文件

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
# Project:pythonMongoDBDemo
# File:6、GridFS.py
# Data:2020/11/9 22:14
# Author:LGSP_Harold
from mongo_db import client
from gridfs import GridFS

db = client.school
gfs = GridFS(db, "book")

file = open("D:/ProgramFiles/Development/PythonWorkspace/pythonMongoDBDemo/python无师自通.pdf", "rb")
args = {"type": "PDF", "keyword": "linux"}
gfs.put(file, filename="python无师自通.pdf", **args)
file.close()


if __name__ == '__main__':
    pass

_2.3.查询GridFS中存储的文件:find和find_one函数可以查询Grid FS中存储的文件;exists判断是否存储某个文件

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
# Project:pythonMongoDBDemo
# File:7、GridFS查询存储的文件_find_one.py
# Data:2020/11/9 22:51
# Author:LGSP_Harold
from mongo_db import client
from gridfs import GridFS
import math

db = client.school

gfs = GridFS(db, collection="book")
book = gfs.find_one({"filename": "python无师自通.pdf"})
print(book.filename)
print(book.type)
print(book.keyword)
print("%dM" % (math.ceil(book.length/1024/1024)))

if __name__ == '__main__':
    pass

 

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
# Project:pythonMongoDBDemo
# File:8、GridFS查询存储的文件_find.py
# Data:2020/11/9 23:53
# Author:LGSP_Harold
from datetime import timedelta

from mongo_db import client
from gridfs import GridFS

db = client.school
gfs = GridFS(db, "book")
books = gfs.find({"type": "PDF"})
for one in books:
    uploadDate = (one.uploadDate + timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S")
    print(one._id, one.filename, uploadDate)

if __name__ == '__main__':
    pass

 

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
# Project:pythonMongoDBDemo
# File:9、GridFS判断是否存储了文件.py
# Data:2020/11/10 0:52
# Author:LGSP_Harold
from mongo_db import client
from gridfs import GridFS
from bson.objectid import ObjectId

db = client.school
gfs = GridFS(db, "book")

rs = gfs.exists(ObjectId("5fa9534ec58627c971fbfc6a"))
print(rs)

rs = gfs.exists(**{"filename": "111.html"})
print(rs)


if __name__ == '__main__':
    pass

 _2.4.读取文件

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
# Project:pythonMongoDBDemo
# File:10、GridFS读取文件.py
# Data:2020/11/10 1:04
# Author:LGSP_Harold
from mongo_db import client
from gridfs import GridFS
from bson.objectid import ObjectId

db = client.school
gfs = GridFS(db, "book")

document = gfs.get(ObjectId("5fa9534ec58627c971fbfc6a"))
file = open("D:/ProgramFiles/Development/PythonWorkspace/pythonMongoDBDemo/python无师自通1.pdf", "wb")
file.write(document.read())
file.close()



if __name__ == '__main__':
    pass

_2.5.删除文件

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
# Project:pythonMongoDBDemo
# File:11、GridFS删除文件.py
# Data:2020/11/10 1:13
# Author:LGSP_Harold
from mongo_db import client
from gridfs import GridFS
from bson.objectid import ObjectId

db = client.school
gfs = GridFS(db, "book")
gfs.delete(ObjectId("5fa9534ec58627c971fbfc6a"))


if __name__ == '__main__':
    pass

 

 

P.S.1:pymongo文档:https://api.mongodb.com/python/current/tutorial.html

posted @ 2020-11-10 22:24  嘆世殘者——華帥  阅读(190)  评论(0编辑  收藏  举报