Mongodb下载
MongoDB的官网是:http://www.mongodb.org/
或者http://dl.mongodb.org/dl/win32/x86_64
Mongodb安装及报错解决方式
下载的安装包解压至某个路径即可。
mongo启动方式:进入mongodb压缩包下bin目录,执行输入mongo启动。如图:
image.pngmongo启动报错解决方式:如果启动报错如下:
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2019-07-18T15:02:35.529+0800 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: :
connect@src/mongo/shell/mongo.js:342:17
解决方式:
-
在mongodb目录下创建\data\db ,\data\log 两个文件夹
linux命令:mkdir -p data/db data/log
image-20200711152933073.png
2.进入bin目录执行mongod命令,上面两个文件夹进行关联。(如果要关联log文件夹,则用--logpath属性.)
image-20200711153054952.png
3、再执行mongo命令,即可看到正常启动.
image-20200711153328326.png
访问:http://127.0.0.1:27017
如果页面出现:It looks like you are trying to access MongoDB over HTTP on the native driver port.则表示正常启动
基本语法
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
> use admin
switched to db admin
> use test --使用不存在的数据库时,mongodb会隐式创建
switched to db test
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
> show collections --查看集合
>
> db.createCollection('c1') --创建简单集合
{ "ok" : 1 }
> db.createCollection('c2')
{ "ok" : 1 }
> show collections
c1
c2
> db.c1.drop() --删除集合
true
> show collections
c2
数据的增删改查
--单条插入
> db.c2.insert({name:"testname",age:"18"})
WriteResult({ "nInserted" : 1 }) --注意:1.插入json格式数据,这里的c2集合如果不存在,也会隐式创建;2.json数据key不加引号,虽然find查看时会有引号.3.会生成一个唯一的_id
> db.c2.find()
{ "_id" : ObjectId("5f09717da7232afeccfee489"), "name" : "testname", "age" : "18" }
--多条插入
> db.c2.insert([{name:"lisi",age:"2"},{name:"zhangs",age:"3"},{name:"wangwu",age:"20"}]) --利用数组一次插入多条数据
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.c2.find()
{ "_id" : ObjectId("5f09717da7232afeccfee489"), "name" : "testname", "age" : "18" }
{ "_id" : ObjectId("5f0973a1a7232afeccfee48a"), "name" : "lisi", "age" : "2" }
{ "_id" : ObjectId("5f0973a1a7232afeccfee48b"), "name" : "zhangs", "age" : "3" }
{ "_id" : ObjectId("5f0973a1a7232afeccfee48c"), "name" : "wangwu", "age" : "20" }
--mongo支持简单的js语法,一次插入多条数据
> for(var i = 1;i <= 10;i++){print(i)}
1
2
3
4
5
6
7
8
9
10
>> for(var i = 1;i <= 10;i++){ db.c3.insert({name:"a"+i,age:i})}
WriteResult({ "nInserted" : 1 }) --执行返回结果1是因为for循环是逐条插入,前面的提示看不到。
> db.c3.find()
{ "_id" : ObjectId("5f0974f2a7232afeccfee48d"), "name" : "a1", "age" : 1 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48e"), "name" : "a2", "age" : 2 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48f"), "name" : "a3", "age" : 3 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee490"), "name" : "a4", "age" : 4 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee491"), "name" : "a5", "age" : 5 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee492"), "name" : "a6", "age" : 6 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee493"), "name" : "a7", "age" : 7 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee494"), "name" : "a8", "age" : 8 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee495"), "name" : "a9", "age" : 9 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee496"), "name" : "a10", "age" : 10 }
查询
基础语法:db.集合名.find(条件 [,查询的列])
等值查询
> db.c3.find({age:3})
{ "_id" : ObjectId("5f0974f2a7232afeccfee48f"), "name" : "a3", "age" : 3 }
--运算符查询 --$gt 大于
$gte 大于等于
$lt 小于
$lte 小于等于
$ne 不等于
$in in
$nin not in
> db.c3.find({age:{$gt:3}})
{ "_id" : ObjectId("5f0974f2a7232afeccfee490"), "name" : "a4", "age" : 4 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee491"), "name" : "a5", "age" : 5 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee492"), "name" : "a6", "age" : 6 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee493"), "name" : "a7", "age" : 7 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee494"), "name" : "a8", "age" : 8 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee495"), "name" : "a9", "age" : 9 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee496"), "name" : "a10", "age" : 10 }
> db.c3.find({age:{$in:[5,8,10]}})
{ "_id" : ObjectId("5f0974f2a7232afeccfee491"), "name" : "a5", "age" : 5 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee494"), "name" : "a8", "age" : 8 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee496"), "name" : "a10", "age" : 10 }
--{age:1} 查询结果只显示该字段,{age:0}显示除了该字段的其他字段
> db.c3.find({age:1},{age:0})
{ "_id" : ObjectId("5f0974f2a7232afeccfee48d"), "name" : "a1" }
> db.c3.find({age:1},{age:1})
{ "_id" : ObjectId("5f0974f2a7232afeccfee48d"), "age" : 1 }
> db.c3.find().pretty() --格式化语句
--转换格式NumberInt
> db.c3.find({age:{$gt:NumberInt(8)}})
-- $and用法
> db.c3.find({$and:[{age:8},{name:"a10"}]})
--$or用法
> db.c3.find({$or:[{age:8},{name:"a10"}]})
{ "_id" : ObjectId("5f0974f2a7232afeccfee494"), "name" : "a8", "age" : 8 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee496"), "name" : "a10", "age" : 10 }
更新
基础语法:db.集合名.update(条件,新数据[,是否新增,是否修改多条])
通过修改器放置值的替换而不是我们需要的修改值
db.集合名.update(条件,新数据)改为
db.集合名.update(条件,{修改器:{键:值}})
修改器有:$inc 递增
$rename 重命名列
$set 修改列值
$unset 删除列
> db.c3.update({name:"a3"},{$set:{name:"a33"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c3.find()
{ "_id" : ObjectId("5f0974f2a7232afeccfee48d"), "name" : "a1", "age" : 1 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48e"), "name" : "a2", "age" : 2 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48f"), "name" : "a33", "age" : 3 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee490"), "name" : "a4", "age" : 4 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee491"), "name" : "a5", "age" : 5 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee492"), "name" : "a6", "age" : 6 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee493"), "name" : "a7", "age" : 7 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee494"), "name" : "a8", "age" : 8 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee495"), "name" : "a9", "age" : 9 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee496"), "name" : "a10", "age" : 10 }
一次修改多个值或属性
> db.c4.insert({name:"渣渣1",age:"18",who:"男",other:"other"})
WriteResult({ "nInserted" : 1 })
> db.c4.find()
{ "_id" : ObjectId("5f0981b8a7232afeccfee497"), "name" : "渣渣1", "age" : "18", "who" : "男", "other" : "other" }
> db.c4.update({name:"渣渣1"},{$set:{name:"渣渣66"},$rename:{who:"set"},$unset:{other:true}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c4.find()
{ "_id" : ObjectId("5f0981b8a7232afeccfee497"), "name" : "渣渣66", "age" : "18", "set" : "男" }
--删除
基本语法:db.集合名.remove(条件 [,是否删除一条])
注意:是否删除一条true是,false否-默认删除多条
> db.c3.find()
{ "_id" : ObjectId("5f0974f2a7232afeccfee48d"), "name" : "a1", "age" : 1 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48e"), "name" : "a2", "age" : 2 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48f"), "name" : "a33", "age" : 3 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee490"), "name" : "a4", "age" : 4 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee491"), "name" : "a5", "age" : 5 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee492"), "name" : "a6", "age" : 6 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee493"), "name" : "a7", "age" : 7 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee494"), "name" : "a8", "age" : 8 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee495"), "name" : "a9", "age" : 9 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee496"), "name" : "a10", "age" : 10 }
> db.c3.remove({age:9})
WriteResult({ "nRemoved" : 1 })
> db.c3.find()
{ "_id" : ObjectId("5f0974f2a7232afeccfee48d"), "name" : "a1", "age" : 1 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48e"), "name" : "a2", "age" : 2 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48f"), "name" : "a33", "age" : 3 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee490"), "name" : "a4", "age" : 4 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee491"), "name" : "a5", "age" : 5 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee492"), "name" : "a6", "age" : 6 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee493"), "name" : "a7", "age" : 7 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee494"), "name" : "a8", "age" : 8 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee496"), "name" : "a10", "age" : 10 }
排序
基本语法:db.集合名.find().sort(JSON数据)
1升序 -1降序
> db.c3.find().sort({age:1})
{ "_id" : ObjectId("5f0974f2a7232afeccfee48d"), "name" : "a1", "age" : 1 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48e"), "name" : "a2", "age" : 2 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48f"), "name" : "a33", "age" : 3 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee490"), "name" : "a4", "age" : 4 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee491"), "name" : "a5", "age" : 5 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee492"), "name" : "a6", "age" : 6 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee493"), "name" : "a7", "age" : 7 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee494"), "name" : "a8", "age" : 8 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee496"), "name" : "a10", "age" : 10 }
> db.c3.find().sort({age:-1})
{ "_id" : ObjectId("5f0974f2a7232afeccfee496"), "name" : "a10", "age" : 10 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee494"), "name" : "a8", "age" : 8 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee493"), "name" : "a7", "age" : 7 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee492"), "name" : "a6", "age" : 6 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee491"), "name" : "a5", "age" : 5 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee490"), "name" : "a4", "age" : 4 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48f"), "name" : "a33", "age" : 3 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48e"), "name" : "a2", "age" : 2 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee48d"), "name" : "a1", "age" : 1 }
分页查询
limit与skip方法
基本语法:db.集合名.find().sort().skip(数字).limit(数字)
> db.c3.find().sort({age:-1}).limit(2)
{ "_id" : ObjectId("5f0974f2a7232afeccfee496"), "name" : "a10", "age" : 10 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee494"), "name" : "a8", "age" : 8 }
> db.c3.find().sort({age:-1}).skip(2).limit(2)
{ "_id" : ObjectId("5f0974f2a7232afeccfee493"), "name" : "a7", "age" : 7 }
{ "_id" : ObjectId("5f0974f2a7232afeccfee492"), "name" : "a6", "age" : 6 }
>
简单的聚合查询
基本语法:db.集合名.aggregate([{管道:{表达式}}])
常用管道:
$group 分组用于统计结果
$match 过滤数据
$sort 聚合数据进一步排序
$skip 跳过指定文档数
$limit 限制集合数据返回文档数
...
常用表达式
$sum 综合 $sum:1同count表示统计
$avg 平均
$min 最小值
$max 最大值
...
db.c4.find()
{ "_id" : ObjectId("5f099afda7232afeccfee498"), "name" : "testname1", "age" : 1, "sex" : 1 }
{ "_id" : ObjectId("5f099afda7232afeccfee499"), "name" : "testname2", "age" : 2, "sex" : 1 }
{ "_id" : ObjectId("5f099afda7232afeccfee49a"), "name" : "testname3", "age" : 3, "sex" : 2 }
{ "_id" : ObjectId("5f099afda7232afeccfee49b"), "name" : "testname4", "age" : 4, "sex" : 2 }
{ "_id" : ObjectId("5f099affa7232afeccfee49c"), "name" : "testname5", "age" : 5, "sex" : 2 }
--统计1/2分别对应的总age
> db.c4.aggregate([{$group:{_id: "$sex",rs:{$sum: "$age"}}}]) --rs是自定义的结果列的名称,_id是固定写法
{ "_id" : 2, "rs" : 12 }
{ "_id" : 1, "rs" : 3 }
--统计1/2对应的个数
> db.c4.aggregate([{$group:{_id: "$sex",rs:{$sum: 1}}}]) --区分并统计总数
{ "_id" : 2, "rs" : 3 }
{ "_id" : 1, "rs" : 2 }
--统计总数,平均年龄
> db.c4.aggregate([{$group:{_id: null,total_num:{$sum:1},total_avg:{$avg:"$age"}}}])
{ "_id" : null, "total_num" : 5, "total_avg" : 3 }
--查询1/2的count数量,按量升序
> db.c4.aggregate([{$group:{_id: "$sex",rs:{$sum: 1}}},{$sort:{rs:1}}])
{ "_id" : 1, "rs" : 2 }
{ "_id" : 2, "rs" : 3 }
> db.c4.aggregate([{$group:{_id: "$sex",rs:{$sum: 1}}},{$sort:{rs:-1}}])
{ "_id" : 2, "rs" : 3 }
{ "_id" : 1, "rs" : 2 }
字段加索引:
--指定name字段加索引(1代表升序,-1代表降序)
> db.c3.createIndex({name:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
--查看集合所有索引,key表示给哪个列设置了索引,name表示索引名称
> db.c3.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.c3"
},
{
"v" : 2,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test.c3"
}
]
>
--创建索引并给新索引命名
> db.c3.createIndex({name:1},{name:"saltindex"})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.c3.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.c3"
},
{
"v" : 2,
"key" : {
"name" : 1
},
"name" : "saltindex",
"ns" : "test.c3"
}
]
>
--删除索引
> db.c3.dropIndex("name_1")
{ "nIndexesWas" : 2, "ok" : 1 }
> db.c3.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.c3"
}
]
--创建组合索引
> db.c3.createIndex({name:1,age:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
--创建唯一性索引
> db.c3.createIndex({name:1},{unique:"name"})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.c3.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.c3"
},
{
"v" : 2,
"unique" : true,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test.c3"
}
]
>
--查询查看执行计划
> db.c3.find({age:8}).explain("executionStats")
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.c3",
"indexFilterSet" : false,
"parsedQuery" : {
"age" : {
"$eq" : 8
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"age" : {
"$eq" : 8
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 0,
"totalKeysExamined" : 0,
"totalDocsExamined" : 9,
"executionStages" : {
"stage" : "COLLSCAN", --COLLSCAN 全表扫描;IXSCAN 索引扫描;FETCH 根据索引去检索指定document
"filter" : {
"age" : {
"$eq" : 8
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 11,
"advanced" : 1,
"needTime" : 9,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"direction" : "forward",
"docsExamined" : 9
}
},
"serverInfo" : {
"host" : "ruanfengdeMBP.lan",
"port" : 27017,
"version" : "4.2.8",
"gitVersion" : "43d25964249164d76d5e04dd6cf38f6111e21f5f"
},
"ok" : 1
}
>
网友评论