MongoDB安装部署
基于windows平台部署MongoDB服务
- 下载解压安装
- 使用配置文件方式启动
- 设置变量
- 。。。
数据库操作
- 查看数据库
> show dbs/databases
- 创建数据库
> use myDB
switched to db myDB
注:如果数据库不存在,则创建数据库。否则切换到该数据库
- 统计数据库信息
> db.stats()
- 删除数据库
> db.dropDatabase() //删除当前数据库
{
"ok" : 1}
> show dbs
admin 0.000GB
Config 0.000GB
local 0.000GB //注:删除当前数据库,默认删除test数据库
集合操作
- 创建集合
//语法:
> db.createCollection(name,{
capped:<Boolean>,autolndexld:<Boolean>,size:<number>,max:<number>
})
name:集合名字
capped:是否启用集合限制,默认没有
size:限制集合使用空间大小,默认没有
max:集合中最大条数限制,默认没有(优先级小于max)
autolndexld:是否使用_id作为索引,默认使用
- 查看集合
> show collections
> db.getCollectionInfos()
- 重命名集合
> db.myCollection(原名称).renameCollection("myColl(新名称)")
{ "ok" : 1 }
- 删除集合
> db.myColl.drop() //db.集合名称.drop
true // 执行成功
- 定长集合
> db.createCollection(name, {capped: <Boolean>,
autoIndexId: <Boolean>,
size: <number>, max <number>} )
capped:是否定长,true为定长
size:集合中文档数量大小(以字节为单位)
Max: 指定集合中允许的最大文档数
> db.myCollection.isCapped() //判断集合是否定长。
定长集合中容量循环使用,用于实时监控
文档操作
- 插入(insert)
> db.collection_name.insert ({
"":"", .. , ,}) //一次性插入一条文档的命令。
> db.collection_name.insertMany([{},{},..]) //一次性插入多条文档的命令。
> db.mydb.insert({
"x":"10"}) //不指定_id的值,自动创建
WriteResult({ "nInserted" : 1 })
> db.mydb.insert({
"_id":"user001","y":"10"}) //指定_id的值,取指定值
WriteResult({ "nInserted" : 1 })
> db.mydb.find() //上面两条命令区别
{ "_id" : ObjectId("5c5ff402eb5725b5d8961b45"), "x" : "10" }
{ "_id" : "user001", "y" : "10" }
- 删除(deleteMany/remove)
> db.collection_name.remove({匹配条件})
> db.collertion_name.deleteMany({匹配条件})
- 更新
当有些内存错误或者是有些内容变更时,需要更新数据库文档
更新单个文档:原子性。
更新多个文档:对每个文档是原子性的,但对整个更新操作,不具备原子性,存在前面的文档更新成功,而后面的更新失败的情况。
同时更新同一文档:需要等待,结果由时间靠后的更新操作决定。
更新方式:update,save
save()
通过传入文档来替换已有文档,_id存在就更新,不在就修改
> db.mycoll.save({ "_id" : ObjectId("**"),"":"",..})
//替换_id为**的文档数据
update()
用于更新已存在的文档
>db.col.update(<query>,<update>,
{
upsert:<boolean>,
multi:<boolean>,
writeConcern:<document>
})
< query >:update的查询条件
< update >:update的对象及一些更新操作符(如:$inc…等)
upsert:若不存在update的记录,是否插入objNew,默认为false不插入
multi:默认为false,只更新找到的第一条数据,若设为true,则可按照条件找出多条数据全部更新
writeConcern:可选,抛出异常的级别
> db.colname.update({匹配字符},{修改器(如:$inc..):{
key:value}},{选项})
// 例:修改多条数据,同时把多个"name":"zs"的“age”改为20(第二个设为true)
> db.myColl.update({
"name":"zs"},{$set:{
"age":20}},false,true)
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })
修改器:
(1) $inc (修改数值,做加法,只能针对数字类型)
(2) $mul (修改数值,做乘法)
(3) $set (完成特定需求的修改,即修改某一值。如果指定的键不存在,则进行添加操作)
(4) $push (数组插入,在文档最后插入,若键不存在,则在文档最后追加数组类型的键值对)
(5) $addToSet (向数组中添加元素,如果存在则不添加)
(7) $unset (删除字段)
(6) $min (给定值与当前文档字段值进行比较,当给定值比较小时,则修改当前文档值为给定值
)
(7) $max (给定值与当前文档字段值进行比较,当给定值比较大时,则修改当前文档值为给定值)
小结
tab键补全(tab不了的前面可能已经错误了)
格式化输入:没输完可以回车后接着
最后退出:quit()
数据库相关操作
创建:use,统计信息:stats(),删除: dropDatabase()
> use test
switched to db test
> db.stats()
{
**//相关信息
"ok" : 1
}
集合相关操作
创建:createCollection,
查看:show collections
显示:getCollectionInfos()
重命名: renameCollection
删除:drop()
> db.createCollection("mycollection")
{ "ok" : 1 }
> show collections
mycollection
> db.getCollectionInfos()
[
{
"name" : "myCollection",
"type" : "collection",
"options" : {
},
"info" : {
** //相关信息
]
> db.mycollection.renameCollection("mycoll")
{ "ok" : 1 }
> db.mycoll.drop()
true
文档相关操作
增入文档:insert 和 insertMany
删除文档: remove 和 deleteMany
更新文档:save 和
update
(修改器:inc,set,push,addToSet,mul,rename,unset,min,max)
//insert 和 insertMany 添加
> db.myColl.insert({
"name":"zhangsan",age:18,gender:"male"}) //添加数据
WriteResult({ "nInserted" : 1 }) //成功
> db.mycoll.find().pretty()
{
"_id" : ObjectId("60**a0"),
"name" : "zs",
"age" : 10,
"gender" : "male"
}
> db.mycoll.insertMany([{
"name":"lisi","age":18,"gender":"male"},{
"name":"zs","age":19,"gender":"male"},{
"name":"ww","age":19,"gender":"female"}]) //添加多条数据
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("**"),
ObjectId("**"),
ObjectId("**")
]
} // 成功
//remove 和 deleteMany 删除
> db.mycoll.remove({
"name":"lisi"}) // 删除“name”为“lisi”的文档
WriteResult({ "nRemoved" : 1 })
> db.mycoll.deleteMany({
"age":19}) //删除所有“age”为19的文档
{ "acknowledged" : true, "deletedCount" : 2 }
//save 更新
> db.myColl.save({
"_id" : ObjectId("60**a0"),"name":"zsf"}) //通过_id更新修改
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
//update 更新
> db.mycoll.insert({
"name":"zs","userId":"user001","age":18,"gender":"male"})
WriteResult({ "nInserted" : 1 }) //原数据添加
> db.mycoll.update({
"name":"zs"},{$inc:{
"age":1}}) // $inc示例,实现age+1
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.myColl.update({
"name":"zhangsan1"},{$mul:{
"age":2}}) //$mul示例,实现age*2
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mycoll.update({
"name":"zs"},{$set:{
"age":20}}) //用 $set 修改
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mycoll.update({
"name":"zs"},{$set:{
"email":["[email protected]"]}}) //用 $set 添加
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mycoll.update({
"name":"zs"},{$push:{
"email":"[email protected]"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
/* email键存着一个数组,用 $push 完成插入,即:
"email" : [
"[email protected]",
"[email protected]"
]
重复 $push 将重复插入
*/
> db.mycoll.update({
"name":"zs"},{$addToSet:{
"email":"[email protected]"}}) //向email数组中添加, $addToSet 示例,已存在所以不改变
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.mycoll.update({
"name":"zs"},{$unset:{
"email":[]}}) //用 $unset 删除字段
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mycoll.update({
"name":"zs"},{$min:{
"age":18}}) //$min,age20→18
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mycoll.update({
"name":"zs"},{$max:{
"age":20}}) //$max,age18→20
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })