一.MongoDB
是一种可扩展的敏捷NoSQL数据库,其中的Mongo源自单词humongous。MongoDB基于NoSQL文档存储模型;在这种模型中,数据对象被存储为集合中的文档,而不是传统关系型数据库中的行和列。文档是以二进制JSON(BSON)对象的方式存储的(端口27017)。
nosql (非关系型数据库)> mongodb > 数据库 > 集合 > 文档
1.库操作
(1)进入(mongo)
(py3env) bd@DF:~$ mongo
(2)退出(exit)
> exit
bye
(3)展示所有数据库(show)
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
(4)查看所在位置(db)
> db
test
(MongoDB 默认进入 test 数据库)
(5)进入别的数据库(use)
> use admin
switched to db admin
(6)给数据库创建内容(createCollection)
Collection 为集合名,进入数据库之后,可以不用创建集合,当插入一个不存在的集合时,会自动创建集合
> db.createCollection('student')
{ "ok" : 1 }
> show dbs
admin 0.000GB
coco 0.000GB
config 0.000GB
local 0.000GB
(7)删除(dropDatabase)
> db.dropDatabase()
{ "dropped" : "coco", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
2.文档操作
(1)插入文档(insert)
> db.student.insert({name:'yige',age:18})
WriteResult({ "nInserted" : 1 })
(2)查看文档(find)
> db.student.find()
{ "_id" : ObjectId("606d9a1e1141bc336205d625"), "name" : "yige", "age" : 18 }
(3)插入单个(insertOne)
> db.student.insertOne({name:'sige',age:25})
{
"acknowledged" : true,
"insertedId" : ObjectId("606d9e8a1141bc336205d628")
}
(4)插入多个(insertMany)
> db.student.insertMany([{name:'wuge',age:17},{name:'liuge',age:23}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("606d9f4b1141bc336205d629"),
ObjectId("606d9f4b1141bc336205d62a")
]
}
(5)格式化查询(pretty)
更为美观
> db.student.find().pretty()
{
"_id" : ObjectId("606d9a1e1141bc336205d625"),
"name" : "yige",
"age" : 18
}
{
"_id" : ObjectId("606d9e351141bc336205d626"),
"name" : "liange",
"age" : 19
}
{
"_id" : ObjectId("606d9e351141bc336205d627"),
"name" : "sange",
"age" : 36
}
{
"_id" : ObjectId("606d9e8a1141bc336205d628"),
"name" : "sige",
"age" : 25
}
{
"_id" : ObjectId("606d9f4b1141bc336205d629"),
"name" : "wuge",
"age" : 17
}
{
"_id" : ObjectId("606d9f4b1141bc336205d62a"),
"name" : "liuge",
"age" : 23
}
(6)带条件查询(等于)
> db.student.find({name:'sange'}).pretty()
{
"_id" : ObjectId("606d9e351141bc336205d627"),
"name" : "sange",
"age" : 36
}
(7)待条件查询(大于,大于等于,小于,小于等于,等于,不等于,用法一样只写一个)
> db.student.find({age:{$gt:18}}).pretty()
{
"_id" : ObjectId("606d9e351141bc336205d626"),
"name" : "liange",
"age" : 19
}
{
"_id" : ObjectId("606d9e351141bc336205d627"),
"name" : "sange",
"age" : 36
}
{
"_id" : ObjectId("606d9e8a1141bc336205d628"),
"name" : "sige",
"age" : 25
}
{
"_id" : ObjectId("606d9f4b1141bc336205d62a"),
"name" : "liuge",
"age" : 23
}
{ "_id" : 7, "name" : "sige", "age" : 25 }
附表:
(8)带条件查询(and)
用逗号进行分隔
> db.student.find({name:'yige',age:18}).pretty()
{
"_id" : ObjectId("606d9a1e1141bc336205d625"),
"name" : "yige",
"age" : 18
}
(9)带条件查询($or)
> db.student.find({$or:[{name:'yige'},{name:'sange'}]}).pretty()
{
"_id" : ObjectId("606d9a1e1141bc336205d625"),
"name" : "yige",
"age" : 18
}
{
"_id" : ObjectId("606d9e351141bc336205d627"),
"name" : "sange",
"age" : 36
}
(10)全文档替换(update)
替换后将会完全按照替换后的来
{
"_id" : ObjectId("606d9a1e1141bc336205d625"),
"name" : "yige",
"age" : 18
}
> db.student.update({name:'yige'},{age:19})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
{ "_id" : ObjectId("606d9a1e1141bc336205d625"), "age" : 19 }
(11)指定属性修改($set)
只会将查询到的第一个进行修改
> db.student.update({age:36},{$set:{age:23}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
(12)对指定属性全部修改(multi)
{
"_id" : ObjectId("606d9e351141bc336205d626"),
"name" : "liange",
"age" : 19
}
{
"_id" : ObjectId("606d9e351141bc336205d627"),
"name" : "sange",
"age" : 23
}
{
"_id" : ObjectId("606d9e8a1141bc336205d628"),
"name" : "sige",
"age" : 25
}
{
"_id" : ObjectId("606d9f4b1141bc336205d629"),
"name" : "wuge",
"age" : 17
}
{
"_id" : ObjectId("606d9f4b1141bc336205d62a"),
"name" : "liuge",
"age" : 23
}
> db.student.update({age:23},{$set:{age:18}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
{
"_id" : ObjectId("606d9e351141bc336205d626"),
"name" : "liange",
"age" : 19
}
{
"_id" : ObjectId("606d9e351141bc336205d627"),
"name" : "sange",
"age" : 18
}
{
"_id" : ObjectId("606d9e8a1141bc336205d628"),
"name" : "sige",
"age" : 25
}
{
"_id" : ObjectId("606d9f4b1141bc336205d629"),
"name" : "wuge",
"age" : 17
}
{
"_id" : ObjectId("606d9f4b1141bc336205d62a"),
"name" : "liuge",
"age" : 18
(13)删除指定全部元素(remove)
> db.student.remove({name:'sige'})
WriteResult({ "nRemoved" : 1 })
(14)删除指定元素的第一个元素(justOne)
> db.student.remove({age:18},{justOne:true})
WriteResult({ "nRemoved" : 1 })
(15)删除全部元素
> db.student.remove({})
WriteResult({ "nRemoved" : 5 })
(16)删除元素并释放空间(repairDatabase)
先使用 remove 进行删除,后使用 repair 进行彻底清空
> db.repairDatabase()
{ "ok" : 1 }
(16)删除一个元素(deleteOne)
> db.student.deleteOne({age:18})
{ "acknowledged" : true, "deletedCount" : 1 }
(17)删除多个元素(deleteMany)
> db.student.deleteMany({age:18})
{ "acknowledged" : true, "deletedCount" : 2 }
3.python 交互
x先在远程连接工具中进行下载 pymongo 模块,(pip install pymongo -i http://pypi.douban.com/simple)
再在 pycharm 中导入 pymongo 模块,实例化 MongoDB 对象创建 mongodb 客户端,连接指定数据库,连接指定集合。就可进行操做了(例如增删改查)