启动服务
1.mongod --dbpath=..\data\db
2.创建mongod.conf
storage:
dbPath: 绝对路径...\data
命令:mongod -f ../conf/mongod.conf
或 mongod --config ../conf/mongod.conf
启动控制台
bin目录下:
mongo --host=127.0.0.1
一些命令
选择/创建数据库:
use 数据库名称
查看数据库:
show dbs
/ show databases
查看当前正在使用的数据库:
db
删除数据库:
db.dropDatabases()
** 集合操作**
查看
show collections
创建
集合类似关系型数据库的表,可显式创建也可隐式创建
显式创建:直接创建
命令(name:创建的集合名称):
db.createCollection("name")
隐式创建:插入文档的时候,如果没有这个集合,就自动创建一个
删除
db.collection.drop()
或 db.集合.drop()
文档基本CURD
增
文档的数据结构和json基本一样,所有储存在集合中的数据都是BSON格式
单个文档的插入
使用insert()或save()
语法:
db.collection.insert(
<document or array of documents>,
{
writeConcern: <document>,
ordered: <boolean>
}
}
Parameter | Type | Description |
---|---|---|
document | document or array | 要插入到集合中的文档或文档数组。(json格式) |
writeConcern | document | 性能损耗性的级别,了解就好 |
ordered | boolean | 可选,如果为真,按顺序插入数组中的文档,如果其中一个出错,将返回而不处理数组中的其余文档。如果为假,执行无需插入,如果出错,急需处理数组中的主文档。认为true |
举例:
db.comment.insert(
{
"articleid":"1000",
"content":"good day",
"userid":"1001",
"nickname":"haozi",
"createdatetime":new Date(),
"likenum":NumberInt(10),
"state":null
}
)
说明:
- 如果comment集合不存在,就会隐式创建
- mongo中的数字,默认是double类型,如果要整型,必须用函数NumberInt(整形数字),否则取出来就会有问题。
- 插入当前日期使用
new Date()
- 插入的数据没有指定_id,会自动生成主键
- 如果某字段没值,可以赋值为null或不写该字段
批量插入
db.collection.insertMany(
[ <document1> , <document2> , ...],
{
writeConcern: <document>,
ordered: <boolean>
}
}
举例:
db.comment.insertMany([
{
"_id":"1",
"articleid":"1001",
"content":"good day",
"userid":"1001",
"nickname":"haozi",
"createdatetime":new Date(),
"likenum":NumberInt(1),
"state":null
},
{
"_id":"2",
"articleid":"1002",
"content":"hello world",
"userid":"1002",
"nickname":"haozi",
"createdatetime":new Date(),
"likenum":NumberInt(2),
"state":null
}
])
查
语法:
db.collection.find(<query> , [projection])
Parameter | Type | Description |
---|---|---|
query | document | 可选。使用查询运算符指定选择筛选器。若要返回集合中的所有文档,省略此参数或传递{} |
projection | document | 可选。指定要在与查询筛选器匹配的文档中返回的字段(投影) |
查询所有
db.comment.find()
或db.comment.find({})
条件查询
db.comment.find({userid:"1001"})
查询一条
多条的时候只要第一条:
db.comment.findOne({userid:"1001"})
投影查询
如果只想要查询结果中的部分字段,而不是所有字段,就用这个。
db.comment.find(
{userid:"1001"},
{userid:1,_id:0}
)
1表示显示,0表示不显示。查询出来的都会把_id主键带上,所以如果不想显示出来,就写0
try-catch
批量插入如果插入失败,会终止插入,但已经插入成功的数据不会回滚,所以批量的时候会有风险,因此可以用try-catch进行异常捕捉。
try{
db.comment.insertMany([
{
"_id":"1",
"articleid":"1001",
"content":"good day",
"userid":"1001",
"nickname":"haozi",
"createdatetime":new Date(),
"likenum":NumberInt(1),
"state":null
},
{
"_id":"2",
"articleid":"1002",
"content":"hello world",
"userid":"1002",
"nickname":"haozi",
"createdatetime":new Date(),
"likenum":NumberInt(2),
"state":null
}
])
}catch (e){
print(e);
}
改
db.collection.update(query, update, options)
//或
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [<filterdocument1>, ...],
hint: <document|string>
}
)
Parameter | Type | Description |
---|---|---|
document | document or array | 要插入到集合中的文档或文档数组。(json格式) |
writeConcern | document | 性能损耗性的级别,了解就好 |
ordered | boolean | 可选,如果为真,按顺序插入数组中的文档,如果其中一个出错,将返回而不处理数组中的其余文档。如果为假,执行无需插入,如果出错,急需处理数组中的主文档。认为true |
覆盖修改
执行覆盖修改后,除修改字段外其他字段都不见了
db.comment.update(
{_id:"2"},
{likenum:NumberInt(1006)}
)
结果:
执行前:
执行后:
局部修改
为了解决覆盖修改的问题,需要使用修改器$set
db.comment.update(
{_id:"2"},
{
$set:{
likenum:NumberInt(666)
}
}
)
批量修改
注意,上面两种如果有多个符合条件的,只会修改第一条,如果想批量修改,需要加{multi:true}
db.comment.update(
{_id:"2"},
{
$set:{
likenum:NumberInt(666)
}
},
{multi:true}
)
值自动增长
如果想针对某个值在原有值基础上新增或减少,关键词是$inc
db.comment.update(
{_id:"2"},
{
$inc:{
likenum:NumberInt(1)
}
}
)
删
语法
db.集合名称.remove(条件)
举例
db.comment.remove({_id:"1"})
//全部删除传空就好,慎用
db.comment.remove({})
查询进阶
统计查询
语法:
db.collection.count(query,options)
Paramter | Type | Description |
---|---|---|
query | document | 查询选择条件 |
options | document | 可选。用于修改计数的额外选项。 |
- 统计所有记录数
db.collection.count()
- 条件查询
db.collection.count({userid:"1001"})
分页查询
关键词:
limit()
:读取指定数量的数据。如果不写,默认20
skip()
:跳过指定数量的数据。如果不写,默认0
db.comment.find().limit(2).skip(0)
排序查询
关键词:
sort()
:1:升序、-1:降序
db.comment.find().sort({userid:-1,likenum:1})
注意:skipt()、limit()、sort()三个执行顺序是sort、skip、limit。和编写顺序无关。
模糊查询(正则查询)
mongodb的模糊查询是通过正则表达式的方式实现的
格式:
db.collection.find({field:/正则表达式/})
注意:正则是js的语法
//查询包含关键词的
db.comment.find({content:/good/})
//查询以某个关键词开头的
db.comment.find({content:/^world/})
比较查询
就是<,<=,>,>=
db.collection.find({field:{$gt : value}}) //field > value
db.collection.find({field:{$lt : value}}) //field < value
db.collection.find({field:{$gte : value}}) //field >= value
db.collection.find({field:{$lte : value}}) //field <= value
db.collection.find({field:{$ne : value}}) //field != value
包含查询
关键词:
$in
:包含
$nin
:不包含
//包含
db.collection.find(
{
userid:{$in:["1001","1002"]}
}
)
//不包含
db.collection.find(
{
userid:{$nin:["1001","1002"]}
}
)
条件连接查询
查询条件有两个以上。关键词:
$and
:与。类似sql中的and
$or
:或。类似sql中的or
格式:
$and:[{},{},{}]
$or:[{},{},{}]
db.comment.find(
{
$and:[
{likenum:{$gte:NumberInt(2)}},
{likenum:{$lt:NumberInt(4)}}
]
}
)