存档文章评论的数据库放到MongoDB中,数据结构参考如下
数据库:articledb
字段名称 | 字段含义 | 字段类型 | 备注 |
---|---|---|---|
_id | ID | Objected或string | 主键 |
articleid | 文章ID | string | |
content | 评论内容 | string | |
userid | 评论人id | string | |
nickname | 评论人昵称 | string | |
createdatetime | 评论发日期时间 | date | |
likenum | 点赞数 | int | |
replynum | 回复数 | int | |
state | 状态 | string | 0:不可见 1:可见 |
parentid | 上级id | string | 如果为0表示为文章顶级评论 |
选择和创建数据库的语法格式
use 数据库名称
如果数据库不存在则自动创建,例如,以下语句创建articledb
数据库
use articledb
查看有权限查看的所有数据库命令
show dbs
或
show databases
注意:在MongoDB中,集合只有在内容插入后才会创建!就是说,创建集合(数据表)后要插入一个文档(记录),集合才会真正创建
查看当前正在使用的数据库命令
db
MongoDB中默认的数据库为test,如果你没有选择数据库,集合将存放在test数据库中。
数据库名必须满足以下条件
有一些数据库名是保留的,可以直接访问这些有特殊作用的数据库
MongoDB删除数据库的语法格式如下
# 先进入到要删除的数据库
db.dropDatabase()
# 主要用来删除已经持久化的数据库
集合,类似于关系型数据库中的表
可以显示的创建,也可以隐式的创建
# 先进入到某个数据库下
db.createCollection(name)
参数说明
例如:创建一个名为mycollection的普通集合
db.createCollection(mycollection)
查看当前库中的表,show tables命令
show collections
或
show tables
集合的命名规范:
集合删除的语法格式如下
db.集合.drop()
返回值
例如:要删除mycollection集合
db.mycollection.drop()
create read update delete
文档(document)的数据结构和JSON基本一样
所有存储在集合中的数据都是BSON格式
db.collection.insert(
<document or array of documents>,
{
writeConcern:<document>,
ordered:<boolean>
}
)
db.comment.insert(
{
"articleid":"10000",
"content":"今天我们来学习mongodb",
"userid":"1001",
"nickname":"www",
"createdatetime":new Date(),
"likenum":NumberInt(10),
"state":null
}
)
WriteResult({"nInserted":1})
db.collection.insertMany(
[<document 1>, <document 2>, ...],
{
writeConcern:<document>,
ordered:<boolean>
}
)
参数
db.comment.insertMany([
{
"_id":"1",
"articleid":"10001",
"content":"今天我们来学习mongodb",
"userid":"1002",
"nickname":"wdw",
"createdatetime":new Date(),
"likenum":NumberInt(10),
"state":"1"
},
{
"_id":"2",
"articleid":"10002",
"content":"今天我们来学习mongodb",
"userid":"1003",
"nickname":"wd",
"createdatetime":new Date(),
"likenum":NumberInt(100),
"state":"1"
},
{
"_id":"3",
"articleid":"10003",
"content":"今天我们来学习mongodb",
"userid":"1004",
"nickname":"wd5",
"createdatetime":new Date(),
"likenum":NumberInt(100),
"state":"1"
}
])
{"acknowleged":true, "nInserteds":["1","2","3"]}
db.collection.find(<query>,[projection])
db.comment.find()
# 查询所有
db.comment.find({})
# 查询所有
db.comment.find().pretty()
# 并且以json格式显示
这里你会发现每条文档会有一个叫_id的字段,这个相当于我们原来关系型数据库中表的主键,当你在插入文档的时候没有指定该字段,MongoDB就会自动创建,其类型是ObjectID类型
如果我们在插入文档记录时指定该字段也可以,其类型可以是ObjectID类型,也可以是MongoDB支持的任意类型。
db.comment.find({userid:"1003"}).pretty()
# 查询指定字段
db.comment.findOne({'state':'1'})
# 只显示查询到的第一个记录
db.comment.find({userid:"1003"},{"likenum":1,"nickname":1}).pretty()
# 只显示likenum和nickname字段,1是显示
db.comment.find({userid:"1002"},{"likenum":0}).pretty()
# 显示的时候隐藏likenum字段,0是不显示
db.comment.find({},{"likenum":1,"nickname":1}).pretty()
# 查询所有,但是只显示likenum和nickname字段
try{
db.comment.inserMany([
{
"_id":"1",
"articleid":"10001",
"content":"今天我们来学习mongodb",
"userid":"1002",
"nickname":"wdw",
"createdatetime":new Date(),
"likenum":NumberInt(10),
"state":"1"
},
{
"_id":"2",
"articleid":"10002",
"content":"今天我们来学习mongodb",
"userid":"1003",
"nickname":"wd",
"createdatetime":new Date(),
"likenum":NumberInt(100),
"state":"1"
},
{
"_id":"3",
"articleid":"10003",
"content":"今天我们来学习mongodb",
"userid":"1004",
"nickname":"wd5",
"createdatetime":new Date(),
"likenum":NumberInt(100),
"state":"1"
}
])
}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>
}
)
db.comment.update({"_id":"1"},{likenum:NumberInt(1001)})
# 修改_id为1的记录,点赞量为1001
db.comment.find({"_id":"1"})
# 查看这条记录,发现update是覆盖修改
db.comment.update({"_id":"2"},{$set:{likenum:NumberInt(889)}})
# 修改_id为2的记录,点赞量为889
db.comment.find({"_id":"2"}).pretty()
# 查看这条记录,发现update是局部修改
db.comment.update({likenum:NumberInt(1000)},{$set:{nickename:"灭霸"}})
db.comment.find({}).pretty()
# 默认情况下只修改第一条数据
db.comment.update({likenum:NumberInt(1000)},{$set:{nickename:"灭霸"}},{multi:true})
# 修改所有符合条件的记录
db.comment.find({}).pretty()
db.comment.update({_id:"3"},{$inc:{likenum:NumberInt(1)}})
# 把_id为3的点赞数加一
db.comment.find({_id:"3"}).pretty()
db.集合名称.remove(条件)
db.comment.remove({})
# 删除comment集合下的所有数据
db.comment.remove({_id:"1"})
# 删除_id=1的记录
db.collection.count(query,options)
db.comment.count()
# 统计comment集合的所有记录数
db.comment.count({userid:"1002"})
# 统计userid为1002的记录条数
可以使用limit()方法来读取指定数量的数据,使用skip()方法来跳过指定数量的数据
db.collection.find().limit(number).skip(number)
db.comment.find().limit(3)
# 获取三条记录
db.comment.find().skip(3)
# 从第四个记录开始获取
db.comment.find().skip(0).limit(2)
# 第一页
db.comment.find().skip(2).limit(2)
# 第二页
db.comment.find().skip(4).limit(2)
# 第三页
sort()方法对数据进行排序,sort()方法可以通过参数指定排序的字段,并使用1和-1来指定排序的方式,其中1为升序排序,而-1是用于降序排序。
db.collection.find().sort({key:1})
或
db.collection.find().sort(排序方式)
对userid降序排序,并对访问量进行升序排序
db.comment.find().sort({userid:-1,likenum:1})
# -1是降序排序,1是升序排序
MongoDB的模糊查询是通过正则表达式的方式实现的,格式为:
db.collection.find({field:/正则表达式/})
或
db.集合.find({字段:/正则表达式/})
db.comment.find({content:/歌声/})
# 查找内容里面包含歌声的
db.comment.find({content:/^什么/})
# 查找以什么开头的
db.comment.find({likenum:{$gt:NumberInt(700)}})
# 查询喜欢数大于700的记录
包含使用$in操作符
db.comment.find({userid:{$in:["1003","1004"]}})
# 查询评论的集合中userid字段包含1003或1004的文档
db.comment.find({userid:{$nin:["1003","1004"]}})
# 不包含
db.comment.find({$and:[{likenum:{$gt:NumberInt(700)}},{likenum:{$lt:NumberInt(2000)}}]})
# 查询评论集合中likenum大于等于700并且小于2000的文档
db.comment.find({$or:[{userid:"1003"},{likenum:{$lt:NumberInt(1000)}}]})
# 查询评论集合中userid为1003,或者点赞数小于1000的文档记录
选择切换数据库:use articledb
插入数据:db.comment.insert({bson数据})
查询所有数据:db.comment.find()
条件查询:db.comment.find({条件})
查询符合条件的第一条记录:db.comment.findOne({条件})
查询符合条件的前几条记录:db.comment.find({条件}).limit(条数)
查询符合条件的跳过的记录:db.comment.find({条件}).skip(条数)
修改数据:db.comment.update({条件},{修改后的数据})或db.comment.update({条件},{$set:{要修改部分的字段:数据}})
修改数据并自增某字段值:db.comment.update({条件},{$inc:{自增的字段:步进值}})
删除数据:db.comment.remove({条件})
统计查询:db.comment.count({条件})
模糊查询:db.comment.find({字段名:/正则表达式/})
条件比较查询:db.comment.find({字段名:{$gt:值}})
包含查询:db.comment.find({字段名:{$in:[值1, 值2]}}) 或db.comment.find({字段名:{$nin:[值1, 值2]}})
条件连接查询:db.comment.find({$and:[{条件1}, {条件2}]}) 或db.comment.find{$or:[{条件1}, {条件2}]}
索引支持在MongoDB中高效地执行查询。如果没有索引,MongoDB必须执行全集合扫描,即扫描集合中的每个文档,以选择与查询语句 匹配的文档。这种扫描全集合的查询效率是非常低的,特别是在处理大量的数据时,查询可以要花费几十秒甚至几分钟,这对网站的性能是非常致命的。
如果查询存在适当的索引,MongoDB可以使用该索引限制必须检查的文档数。索引是特殊的数据结构,它以易于遍历的形式存储集合数据集的一小部分。索引存储特定字段或一组字段的值,按字段值排序。索引项的排序支持有效的相等匹配和基于范围的查询操作。此外,MongoDB还可以使用索引中的排序返回排序结果
db.collection.getIndexes()
db.comment.getIndexes()
db.comment.createIndex(keys, options)
选项 | 类型 | 描述 |
---|---|---|
background | 布尔 | 是否再后台执行创建索引的过程,不阻塞对集合的操作false【默认】 |
unique | 布尔 | 是否创建具有唯一性的索引false【默认】 |
name | 字符串 | 自定义索引名称,如果不知道,mongodb将通过下划线连接索引字段的名称和排序规则 生成一个索引名称。一旦创建不能修改,只能删除再重新创建 |
partialFilterExpression | Document | 仅为集合中复合条件的文档建立索引,降低创建和维护成本 |
sparse | 布尔 | 仅为集合中具有指定字段的文档建立索引false【默认】 |
expireAfterSeconds | integer单位秒 | 用于TTL索引中控制文档保存在集合中的时间 |
storageEngine | Document | 指定存储引擎配置 |
db.comment.createIndex({userid:1})
db.comment.getIndexes()
db.comment.createIndex({userid:1,nickname:-1})
db.comment.getIndexes()
db.collection.dropIndex(index)
db.comment.dropIndex({userid:1})
db.comment.getIndexes()
db.comment.dropIndex()
# 删除所有索引
分析查询性能通常使用执行计划(解释计划、Explain Plan)来查看查询的情况,如查询耗费的时间、是否基于索引查询等。
db.collection.find(query,options).explain(options)
db.comment.find({userid:"1003"}).explain()
# stage:
db.comment.createIndex({userid:1})
db.comment.find({userid:"1003"}).explain()
当查询条件和查询的投影仅包含索引字段时,MongoDB直接从索引返回结果,而不是扫描任何文档或将文档带入内存。这些覆盖的查询可以非常有效
db.comment.find({userid:"1003"},{userid:1,_id:0})