mongodb基本命令

关闭服务

db.shutdownServer() #通用
systemctl status mongod.service #适用于apt安装的

1. 选择和创建数据库

  • 数据库的创建和进入数据库都是一个命令
use 库名
  • 创建一个数据库名为articledb
use articledb

注意mongodb的存储方式,只创建库没有数据时候,这个时候数据库是存在内存中,只有数据库中有内容才会存放到磁盘,所以创建完没有数据的库使用show dbs是看不到库的。

  • 查看权限内所有的数据库
show dbs
或者
show databases
  • 查看当前正在使用的数据库
db

有一些数据库名是保留的,可以直接访问这些有特殊作用的数据库

  • admin: 从权限的角度来看,这是"root"数据库。要是将一个用户添加到这个数据库,这个用户自动继承所有数据库的权限。一些特定的服务器端命令也只能从这个数据库运行,比如列出所有数据库或者关闭服务器。
  • local: 这个数据永远不会被复制,可以用来存储限于本地单台服务器的任意集合
  • config: 当mongo用于分片设置时,config数据库在内部使用,用于保存分片的相关信息

2. 数据库的删除

db.dropDatabase()

3. 集合操作

3.1 集合的显式创建

基本语法格式

db.createCollection(集合名称)
  • 创建一个名为mycollection的普通集合
db.createCollection(mycollection)
  • 查看当前库中的集合(表)
show collections
或
show tables
3.2 集合的隐形创建

当向一个集合插入一个文档的时候,如果集合不存在,则自动创建集合
具体看文档插入部分
通常我们使用隐式创建文档

3.3 删除集合

基本语法格式

db.集合名.drop()
  • 删除mycollection集合
db.mycollection.drop()

4. 文档的基本操作(增删改查)

4.1 文档的插入
  • 单个文档插入
    创建一个comment的集合,并插入文档
db.comment.insert({"articleid":"10000","content":"今天天气真好,阳光明媚","userid":"1001","nickname":"Rose","createdatetime":new Date(),"likenum":NumberInt(10),"state":null})

提示:
1)comment集合如果不存在,则会自动创建
2)mongo中的数字,默认情况下是double类型,如果要存整型,必须使用函数NumberInt(整型数字),否则取出来就有问题了
3)插入当前日期使用new Date()
4)插入的数据没有指定_id,会自动生成主键值
5)如果某个字段没值,可以赋值为null,或者不写该字段

  • 批量插入
    批量插入多条文章评论:
> db.comment.insertMany([{"articleid":"10001","content":"Are you ok","userid":"1002","nickname":"alonzo","createdatetime":new Date(),"likenum":NumberInt(1000),"state":"1"},
{"articleid":"10001","content":"3q","userid":"1005","nickname":"leijun","createdatetime":new Date(),"likenum":NumberInt(888),"state":"1"},
{"articleid":"10001","content":"very match,阳光明媚","userid":"mifans","nickname":"alonzo","createdatetime":new Date(),"likenum":NumberInt(666),"state":"1"},
{"articleid":"10001","content":"hello mifans","userid":"1003","nickname":"zhangsan","createdatetime":new Date(),"likenum":NumberInt(777),"state":"1"},
{"articleid":"10001","content":"base","userid":"1003","nickname":"zhangsan","createdatetime":new Date(),"likenum":NumberInt(777),"state":"1"}])
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("604ac2298b964c15b2b578bd"),
        ObjectId("604ac2298b964c15b2b578be"),
        ObjectId("604ac2298b964c15b2b578bf"),
        ObjectId("604ac2298b964c15b2b578c0"),
        ObjectId("604ac2298b964c15b2b578c1")
    ]
}

学过Python应该就看出来,这里实际就是将多个字典插入列表里,形成一个表的多条数据

4.2 文档的查询

语法

db.文档名.find()
或
db.文档名.find({})
> db.comment.find()
{ "_id" : ObjectId("6049d65505bd0c812f09cec6"), "articleid" : "10000", "content" : "今天天气真好,阳光明媚", "userid" : "1001", "nickname" : "Rose", "createdatetime" : ISODate("2021-03-11T08:35:33.041Z"), "likenum" : 10, "state" : null }
{ "_id" : ObjectId("604ac2298b964c15b2b578bd"), "articleid" : "10001", "content" : "Are you ok", "userid" : "1002", "nickname" : "alonzo", "createdatetime" : ISODate("2021-03-12T01:21:45.418Z"), "likenum" : 1000, "state" : "1" }
{ "_id" : ObjectId("604ac2298b964c15b2b578be"), "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-12T01:21:45.418Z"), "likenum" : 888, "state" : "1" }
{ "_id" : ObjectId("604ac2298b964c15b2b578bf"), "articleid" : "10001", "content" : "very match,阳光明媚", "userid" : "mifans", "nickname" : "alonzo", "createdatetime" : ISODate("2021-03-12T01:21:45.418Z"), "likenum" : 666, "state" : "1" }
{ "_id" : ObjectId("604ac2298b964c15b2b578c0"), "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-12T01:21:45.418Z"), "likenum" : 777, "state" : "1" }
{ "_id" : ObjectId("604ac2298b964c15b2b578c1"), "articleid" : "10001", "content" : "base", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-12T01:21:45.418Z"), "likenum" : 777, "state" : "1" }
  • 条件查询,比如查询articleid为10001的记录
> db.comment.find({articleid:"10001"})
{ "_id" : ObjectId("604ac2298b964c15b2b578bd"), "articleid" : "10001", "content" : "Are you ok", "userid" : "1002", "nickname" : "alonzo", "createdatetime" : ISODate("2021-03-12T01:21:45.418Z"), "likenum" : 1000, "state" : "1" }
{ "_id" : ObjectId("604ac2298b964c15b2b578be"), "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-12T01:21:45.418Z"), "likenum" : 888, "state" : "1" }
{ "_id" : ObjectId("604ac2298b964c15b2b578bf"), "articleid" : "10001", "content" : "very match,阳光明媚", "userid" : "mifans", "nickname" : "alonzo", "createdatetime" : ISODate("2021-03-12T01:21:45.418Z"), "likenum" : 666, "state" : "1" }
{ "_id" : ObjectId("604ac2298b964c15b2b578c0"), "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-12T01:21:45.418Z"), "likenum" : 777, "state" : "1" }
{ "_id" : ObjectId("604ac2298b964c15b2b578c1"), "articleid" : "10001", "content" : "base", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-12T01:21:45.418Z"), "likenum" : 777, "state" : "1" }
  • 如上条件,但只想返回第一条数据
> db.comment.findOne({articleid:"10001"})
{
    "_id" : ObjectId("604ac2298b964c15b2b578bd"),
    "articleid" : "10001",
    "content" : "Are you ok",
    "userid" : "1002",
    "nickname" : "alonzo",
    "createdatetime" : ISODate("2021-03-12T01:21:45.418Z"),
    "likenum" : 1000,
    "state" : "1"
}
  • 投影查询
    查询nickname为alonzo,只显示userid且不显示_id:
> db.comment.find({nickname:"alonzo"},{userid:1,_id:0})
{ "userid" : "1002" }
{ "userid" : "mifans" }

查询结果只显示userid、nickname不显示_id:

> db.comment.find({},{userid:1,nickname:1,_id:0})
{ "userid" : "1001", "nickname" : "Rose" }
{ "userid" : "1002", "nickname" : "alonzo" }
{ "userid" : "1005", "nickname" : "leijun" }
{ "userid" : "mifans", "nickname" : "alonzo" }
{ "userid" : "1003", "nickname" : "zhangsan" }
{ "userid" : "1003", "nickname" : "zhangsan" }

实际上投影查询就是查询表中的列可以理解为db.comment.find({行筛选},{列筛选})
行筛选需要指定具体数据,列筛选指定0和1,0为不显示,1为显示

  • 由于批量插入数据较多容易出现失败,因此,可以使用try,catch进行异常处理,测试的时候可以不处理
try {
db.comment.insertMany([{"articleid":"10001","content":"Are you ok","userid":"1002","nickname":"alonzo","createdatetime":new Date(),"likenum":NumberInt(1000),"state":"1"},
{"articleid":"10001","content":"3q","userid":"1005","nickname":"leijun","createdatetime":new Date(),"likenum":NumberInt(888),"state":"1"},
{"articleid":"10001","content":"very match,阳光明媚","userid":"mifans","nickname":"alonzo","createdatetime":new Date(),"likenum":NumberInt(666),"state":"1"},
{"articleid":"10001","content":"hello mifans","userid":"1003","nickname":"zhangsan","createdatetime":new Date(),"likenum":NumberInt(777),"state":"1"},
{"articleid":"10001","content":"base","userid":"1003","nickname":"zhangsan","createdatetime":new Date(),"likenum":NumberInt(777),"state":"1"}])
} catch (e) {
    print(e)
}

目前没怎么理解这个功能

4.3 文档的更新

更新文档的语法:

db.collection.update(query,update,options)
  • 覆盖的修改
    修改userid为1002的记录,点赞量为1001,输入以下语句:
> db.comment.update({userid:"1002"},{likenum:NumberInt(1001)})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.comment.find()
{ "_id" : ObjectId("604ad16235ffd35bf6985785"), "likenum" : 1001 }
{ "_id" : ObjectId("604ad16235ffd35bf6985786"), "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 888, "state" : "1" }
{ "_id" : ObjectId("604ad16235ffd35bf6985787"), "articleid" : "10001", "content" : "very match,阳光明媚", "userid" : "mifans", "nickname" : "alonzo", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 666, "state" : "1" }
{ "_id" : ObjectId("604ad16235ffd35bf6985788"), "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 777, "state" : "1" }
{ "_id" : ObjectId("604ad16235ffd35bf6985789"), "articleid" : "10001", "content" : "base", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 777, "state" : "1" }

可以看到这条更新修改把整个数据都给覆盖了,重新生成了之前修改的部分

  • 局部修改
    为了解决这个问题,需要使用$set来实现,之前添加文档的时候细心的朋友可能发现有个userid的值是mifans,这个是错误的,现在想把它改为1004,然后nickname改为mifans,命令如下:
> db.comment.update({userid:"mifans"},{$set:{userid:"1004",nickname:"mifans"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.comment.find({userid:"1004"})
{ "_id" : ObjectId("604ad16235ffd35bf6985787"), "articleid" : "10001", "content" : "very match,阳光明媚", "userid" : "1004", "nickname" : "mifans", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 666, "state" : "1" }
  • 批量的修改
    更新所有用户为1003的用户昵称为lisi
#默认只修改第一条数据
> db.comment.update({userid:"1003"},{$set:{nickname:"lisi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.comment.find({userid:"1003"})
{ "_id" : ObjectId("604ad16235ffd35bf6985788"), "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "lisi", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 777, "state" : "1" }
{ "_id" : ObjectId("604ad16235ffd35bf6985789"), "articleid" : "10001", "content" : "base", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 777, "state" : "1" }
#修改所有符合条件的数据(nickname改为lisi2来做区分)
> db.comment.update({userid:"1003"},{$set:{nickname:"lisi2"}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.comment.find({userid:"1003"})
{ "_id" : ObjectId("604ad16235ffd35bf6985788"), "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "lisi2", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 777, "state" : "1" }
{ "_id" : ObjectId("604ad16235ffd35bf6985789"), "articleid" : "10001", "content" : "base", "userid" : "1003", "nickname" : "lisi2", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 777, "state" : "1" }

所以只要后面不加参数{multi:true}则只更新第一条数据

  • 列值增长的修改
    如果我们想实现对某列值在原有值的基础上的进行增加或减少,可以使用$inc运算符来实现。
    下面是对userid为1003的点赞数进行递增操作,每次递增2
> db.comment.find({userid:"1003"})
{ "_id" : ObjectId("604ad16235ffd35bf6985788"), "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "lisi2", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 777, "state" : "1" }
{ "_id" : ObjectId("604ad16235ffd35bf6985789"), "articleid" : "10001", "content" : "base", "userid" : "1003", "nickname" : "lisi2", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 777, "state" : "1" }
> db.comment.update({nickname:"lisi2"},{$inc:{likenum:NumberInt(2)}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.comment.find({nickname:"lisi2"})
{ "_id" : ObjectId("604ad16235ffd35bf6985788"), "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "lisi2", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 779, "state" : "1" }
{ "_id" : ObjectId("604ad16235ffd35bf6985789"), "articleid" : "10001", "content" : "base", "userid" : "1003", "nickname" : "lisi2", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 779, "state" : "1" }
4.4 删除文档

删除文档的语法结构:

db.集合名称.remove(条件)

以下语句可以讲数据全部删除,请慎用

db.comment.remove({})
  • 如果删除userid为1003的记录,输入以下语句
> db.comment.find()
{ "_id" : ObjectId("604ad16235ffd35bf6985785"), "likenum" : 1001 }
{ "_id" : ObjectId("604ad16235ffd35bf6985786"), "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 888, "state" : "1" }
{ "_id" : ObjectId("604ad16235ffd35bf6985787"), "articleid" : "10001", "content" : "very match,阳光明媚", "userid" : "1004", "nickname" : "mifans", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 666, "state" : "1" }
{ "_id" : ObjectId("604ad16235ffd35bf6985788"), "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "lisi2", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 779, "state" : "1" }
{ "_id" : ObjectId("604ad16235ffd35bf6985789"), "articleid" : "10001", "content" : "base", "userid" : "1003", "nickname" : "lisi2", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 779, "state" : "1" }
> db.comment.remove({userid:"1003"})
WriteResult({ "nRemoved" : 2 })
> db.comment.find()
{ "_id" : ObjectId("604ad16235ffd35bf6985785"), "likenum" : 1001 }
{ "_id" : ObjectId("604ad16235ffd35bf6985786"), "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 888, "state" : "1" }
{ "_id" : ObjectId("604ad16235ffd35bf6985787"), "articleid" : "10001", "content" : "very match,阳光明媚", "userid" : "1004", "nickname" : "mifans", "createdatetime" : ISODate("2021-03-12T02:26:42.644Z"), "likenum" : 666, "state" : "1" }
  • 删除comment里的全部数据
> db.comment.remove({})
WriteResult({ "nRemoved" : 3 })
> db.comment.find()
>

5. 文档的分页查询

首先先将上面删除的数据给创建出来

#创建初始数据
try {
db.comment.insertMany([{_id:"1","articleid":"10001","content":"Are you ok","userid":"1002","nickname":"alonzo","createdatetime":new Date(),"likenum":NumberInt(1000),"state":"1"},
{_id:"2","articleid":"10001","content":"3q","userid":"1005","nickname":"leijun","createdatetime":new Date(),"likenum":NumberInt(888),"state":"1"},
{_id:"3","articleid":"10001","content":"very match,阳光明媚","userid":"1004","nickname":"mifans","createdatetime":new Date(),"likenum":NumberInt(666),"state":"1"},
{_id:"4","articleid":"10001","content":"hello mifans","userid":"1003","nickname":"zhangsan","createdatetime":new Date(),"likenum":NumberInt(777),"state":"1"},
{_id:"5","articleid":"10001","content":"base","userid":"1003","nickname":"zhangsan","createdatetime":new Date(),"likenum":NumberInt(777),"state":"1"}])
} catch (e) {
    print(e)
}
#查询一下
> db.comment.find()
{ "_id" : "1", "articleid" : "10001", "content" : "Are you ok", "userid" : "1002", "nickname" : "alonzo", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 1000, "state" : "1" }
{ "_id" : "2", "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 888, "state" : "1" }
{ "_id" : "3", "articleid" : "10001", "content" : "very match,阳光明媚", "userid" : "1004", "nickname" : "mifans", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 666, "state" : "1" }
{ "_id" : "4", "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }
{ "_id" : "5", "articleid" : "10001", "content" : "base", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }
5.1 统计查询

统计查询使用count方法,语法如下

db.comment.count(query,option)
  • 例如统计comment中所有记录数:
> db.comment.count()
5
  • 统计userid为1003的记录数
> db.comment.count({userid:"1003"})
2
5.2 分页列表查询

可以使用limit()方法来读取指定数量的数据,使用skip()方法来跳过指定数量的数据
基本语法如下所示:

db.集合名.find().limit().skip()
  • 返回指定条数的记录,可以在find方法后调用limit来返回结果(TopN),默认值20,如下查看前三条数据:
> db.comment.find().limit(3)
{ "_id" : "1", "articleid" : "10001", "content" : "Are you ok", "userid" : "1002", "nickname" : "alonzo", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 1000, "state" : "1" }
{ "_id" : "2", "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 888, "state" : "1" }
{ "_id" : "3", "articleid" : "10001", "content" : "very match,阳光明媚", "userid" : "1004", "nickname" : "mifans", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 666, "state" : "1" }
  • skip方法同样接受一个数字参数作为跳过记录条数,即前N个不要,默认值是0,如下查询时跳过前三条数据:
> db.comment.find().skip(3)
{ "_id" : "4", "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }
{ "_id" : "5", "articleid" : "10001", "content" : "base", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }
  • 分页查询:要求:每页两个,第二页开始:跳过前两条数据,接着显示3和4条数据,以此类推
#显示第一页,每页两条数据
> db.comment.find().skip().limit(2)
{ "_id" : "1", "articleid" : "10001", "content" : "Are you ok", "userid" : "1002", "nickname" : "alonzo", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 1000, "state" : "1" }
{ "_id" : "2", "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 888, "state" : "1" }
#显示第二页,每页两条数据
> db.comment.find().skip(2).limit(2)
{ "_id" : "3", "articleid" : "10001", "content" : "very match,阳光明媚", "userid" : "1004", "nickname" : "mifans", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 666, "state" : "1" }
{ "_id" : "4", "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }
#显示第三页,每页两条数据
> db.comment.find().skip(4).limit(2)
{ "_id" : "5", "articleid" : "10001", "content" : "base", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }

可以看到显示第三页的时候数据不足两条,则只返回一条

5.3 排序查询

sort()方法对数据进行排序,sort()方法可以通过参数指定排序字段,并使用1和-1来指定排序的方式,其中1为升序排序,-1为降序排序
语法如下:

db.集合名.find().sort({KEY1:1,KEY2:-1})
  • 对userid升序排序,并对点赞数进行降序排序
> db.comment.find({},{userid:1,likenum:1}).sort({userid:1,likenum:-1})
{ "_id" : "1", "userid" : "1002", "likenum" : 1000 }
{ "_id" : "4", "userid" : "1003", "likenum" : 777 }
{ "_id" : "5", "userid" : "1003", "likenum" : 777 }
{ "_id" : "3", "userid" : "1004", "likenum" : 666 }
{ "_id" : "2", "userid" : "1005", "likenum" : 888 }

这里数据降序看不出来,理论上userid为1003的那两个数据likenum会进行降序排列

6. 文档的更多查询

6.1正则的复杂条件查询

Mongodb的模糊查询是通过正则表达式的方式实现的,格式为:

db.集合名.find({字段:/正则表达式/})

提示:正则是js的语法

  • 现在要查询评论内容包含“阳光明媚”的所有文档
#因为上面创建的文档只有一条记录有“阳光明媚”,所以再更新其他一条让它有两条该数据,方便我们查询
> db.comment.update({_id:"5"},{$set:{content:"base,阳光明媚"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.comment.find({_id:"5"})
{ "_id" : "5", "articleid" : "10001", "content" : "base,阳光明媚", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }

#查询评论中包含“阳光明媚”的所有文档
> db.comment.find({content:/阳光明媚/})
{ "_id" : "3", "articleid" : "10001", "content" : "very match,阳光明媚", "userid" : "1004", "nickname" : "mifans", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 666, "state" : "1" }
{ "_id" : "5", "articleid" : "10001", "content" : "base,阳光明媚", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }
  • 查询以数字开头的评论
> db.comment.find({content:/^[0-9]/})
{ "_id" : "2", "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 888, "state" : "1" }
6.2 比较查询

<,<=,>,>=这些操作符也是很常用的,格式如下:

db.集合名.find({字段:{$gt:数值}})  #大于
db.集合名.find({字段:{$lt:数值}})  #小于
db.集合名.find({字段:{$gte:数值}})  #大于等于
db.集合名.find({字段:{$lte:数值}})  #小于等于
db.集合名.find({字段:{$ne:数值}})  #不等于
  • 查询评论点赞数大于700的记录
> db.comment.find({likenum:{$gt:700}})
{ "_id" : "1", "articleid" : "10001", "content" : "Are you ok", "userid" : "1002", "nickname" : "alonzo", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 1000, "state" : "1" }
{ "_id" : "2", "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 888, "state" : "1" }
{ "_id" : "4", "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }
{ "_id" : "5", "articleid" : "10001", "content" : "base,阳光明媚", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }
6.3 包含查询

包含使用$in操作符

  • 查询评论的集合中userid字段包含1003和1004的文档
> db.comment.find({userid:{$in:["1003","1004"]}})
{ "_id" : "3", "articleid" : "10001", "content" : "very match,阳光明媚", "userid" : "1004", "nickname" : "mifans", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 666, "state" : "1" }
{ "_id" : "4", "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }
{ "_id" : "5", "articleid" : "10001", "content" : "base,阳光明媚", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }
  • 不包含使用$nin操作符
    查询评论集合中userid字段不包含1003和1004的文档
> db.comment.find({userid:{$nin:["1003","1004"]}})
{ "_id" : "1", "articleid" : "10001", "content" : "Are you ok", "userid" : "1002", "nickname" : "alonzo", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 1000, "state" : "1" }
{ "_id" : "2", "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 888, "state" : "1" }
6.4 条件连接查询

我们如果需要查询同时满足两个以上条件,需要使用$and操作符进行关联。(相当于SQL里的and)
格式为:

db.集合名.find({$and:[{ },{ },{ }]})
  • 查询评论集合中likenum大于等于700且小于2000的记录
> db.comment.find({$and:[{likenum:{$gte:NumberInt(700)}},{likenum:{$lt:NumberInt(2000)}}]})
{ "_id" : "1", "articleid" : "10001", "content" : "Are you ok", "userid" : "1002", "nickname" : "alonzo", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 1000, "state" : "1" }
{ "_id" : "2", "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 888, "state" : "1" }
{ "_id" : "4", "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }
{ "_id" : "5", "articleid" : "10001", "content" : "base,阳光明媚", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }

如果两个以上条件之间是或者的关系,我们使用操作符进行关联,与前面and的使用方式相同格式为:

db.集合名.find({$or:[{ },{ },{ }]})

查询评论集合中userid为1003,或者点赞数小于1000的文档记录:

> db.comment.find({$or:[{userid:"1003"},{likenum:{$lt:NumberInt(1000)}}]})
{ "_id" : "2", "articleid" : "10001", "content" : "3q", "userid" : "1005", "nickname" : "leijun", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 888, "state" : "1" }
{ "_id" : "3", "articleid" : "10001", "content" : "very match,阳光明媚", "userid" : "1004", "nickname" : "mifans", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 666, "state" : "1" }
{ "_id" : "4", "articleid" : "10001", "content" : "hello mifans", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }
{ "_id" : "5", "articleid" : "10001", "content" : "base,阳光明媚", "userid" : "1003", "nickname" : "zhangsan", "createdatetime" : ISODate("2021-03-14T02:39:11.573Z"), "likenum" : 777, "state" : "1" }

7. 常用命令小结

选择切换数据库:use articledb
插入数据:db.comment.insert({bson数据})
查询所有数据:db.comment.find()
条件查询数据:db.comment.find({条件})
查询符合条件的第一条记录:db.comment.findOne({条件})
查询符合条件的前n条数据:db.comment.find.({条件}).limit(n)
查询第n条数据后的数据:db.comment.find({条件}).skip(n)
修改数据: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({$and:[{条件1},{条件2}]})
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,904评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,581评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,527评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,463评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,546评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,572评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,582评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,330评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,776评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,087评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,257评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,923评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,571评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,192评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,436评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,145评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,127评论 2 352

推荐阅读更多精彩内容