当前位置: 首页>数据库>正文

mongodb aggregation 全局求数量 mongodb统计集合中文档数量

目录

MongoDB 常用操作

数据库与集合操作

文档操作

向集合添加文档

删除集合中文档

查询集合中文档

更新集合中文档


本文导读

本文承接《MongoDB 下载_安装_配置 及 启动与连接》,启动 MongoDB 与 连接 MongoDB 了解之后,现在来掌握 MongoDB 的操作语法。

MongoDB 常用操作

命令

作用

举例

 mongod --dbpath=XXX

启动 MongoDB 数据库,XXX为数据库文件存放的物理路径

mongod --dbpath=D:\MongoDB\Data

mongo localhost:27017

连接 MongoDB 数据库

mongo、mongo localhost:27017、mongo ip:port

show dbs

列出所有数据库

show dbs

db

查看当前所在数据库

db

use XXX

切换到某个已存在的数据库

use test

use YYY

当 “YYY” 数据库不存在时,则为新建数据库

use mydb1

db.dropDatabase()

删除当前数据库

新进入待删除的数据库,然后执行 db.dropDatabase()

db.createCollection(“集合名称")

显示创建集合

db.createCollection(“c1")

db.集合名称.insert({})

隐式创建集合

db.集合名称.insert({name:"zhangSan",age:25})

show collections

查看当前数据库中的所有集合

 

 show tables

查看当前数据库中的所有集合

 

db.集合名称.drop( )

删除集合

删除当前数据库下的指定集合,db.c1.drop( )

db.集合名称.insert({})

向集合中添加文档

db.user1.insert({name:”jack”,age:20})

 db.集合名称.save({}) 

向集合中添加文档

db.c1.save({name:"zhangSan",age:25}) 

db.集合名称.remove({删除条件})

删除集合中的文档

db.c1.remove({age:25})

db.集合名称.find({条件})

根据条件查询集合中的文档

db.c1.find({age:25})

db.集合名称.find() 

表示查询所有

db.c1.find()

db.集合名称.findOne() 

查询第一个文档

db.c1.findOne() 

更多查询命令,请直接参考下面的查询章节......

数据库与集合操作

列出所有数据库:show dbs,如下所示之所以没有显示 test 库,是因为此时 test 库中没有内容,并不是不存在 test 库。

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
>

查看当前所在数据库:db

> db
test
>

使用某个数据库:use 已存在的数据库名字,如果数据库名称不存在,则会创建新数据库。

> use local
switched to db local
> db
local
>

创建数据库:使用命令 use 不存在数据库名称

use 命令后跟的数据库名,如果存在就进入此数据库,如果不存在就创建,所以这种创建方式又叫隐式创建

注意:使用命令 use mydb1 创建数据库后,并没有真正生成对应的数据文件,此时还只是存在于存储中,如果此时退出,此数据库将被删除,只有在此数据库中创建集合后,才会真正生成数据文件

> use mydb1
switched to db mydb1
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
>

删除当前数据库:使用命令 db.dropDatabase(),先进入待删除的数据库,然后进行删除。

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mydb1   0.000GB
> db
mydb1
> db.dropDatabase()
{ "dropped" : "mydb1", "ok" : 1 }
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

关闭数据库:先使用 use admin 切换数据库,然后输出 db.shutdownServer() 关闭数据库实例

当然也可以简单粗暴的直接关闭 数据库的 cmd 窗口

> use admin
switched to db admin
> db.shutdownServer()
server should be down...
2018-09-11T16:41:37.684+0800 I NETWORK  [js] trying reconnect to localhost:27017 failed
2018-09-11T16:41:38.751+0800 I NETWORK  [js] reconnect localhost:27017 failed failed
>

创建集合:有两种方式,显示创建 和 隐式创建

显示创建可以使用命令 db.createCollection(“集合名称")

隐式创建可使用命令 db.集合名称.insert({}),创建集合并同时向集合中插入数据,例如:db.customer.insert({name:”jack”})

> use mydb1
switched to db mydb1
> db
mydb1
> db.createCollection("C1")
{ "ok" : 1 }
> show collections
C1
> db.C2.insert({"name":"zhangSan"})
WriteResult({ "nInserted" : 1 })
> show tables
C1
C2
>

查看当前数据库中所有的集合:使用命令 show collections 或使用 show tables

> db
mydb1
> db.createCollection("C1")
{ "ok" : 1 }
> show collections
C1
> show tables
C1
>

删除集合:删除当前数据库下的指定集合,使用命令:db.集合名称.drop( )

> db
mydb1
> db.createCollection("c1")
{ "ok" : 1 }
> show tables
c1
> db.c1.drop()
true
> show tables
> db.c2.insert({"nmae":"张三"})
WriteResult({ "nInserted" : 1 })
> show tables                })
c2
> db.c2.drop();              })
true
> show tables
>

文档操作

向集合添加文档

向集合添加文档:使用命令 db.集合名称.insert({}),例如:db.user1.insert({name:”jack”,age:20})

"_id" 相当于 Mysql 数据库的主键,MongoDB 自动生成!,find 方法执行查询操作!

> db.c1.insert({name:"zhangSan",age:"22"})
WriteResult({ "nInserted" : 1 })
> show tables
c1
> db.c1.insert({name:"zhangSan",age:"25"})
WriteResult({ "nInserted" : 1 })
> db.c1.find()
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan", "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan", "age" : "25" }
>

MongoDB 自带简洁但功能强大的 JavaScript shell,这个工具对于管理 MongoDB 实例和操作数据作用非常大,如下所示使用 JavaScript 的 for 循环进行批量插入。

> db.c1.find()
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan", "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan", "age" : "25" }
> for(var i=1;i<=10;i++){
... db.c1.insert({name:"zhangSan"+i,age:i})
... }
WriteResult({ "nInserted" : 1 })
> db.c1.find()
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan", "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan", "age" : "25" }
{ "_id" : ObjectId("5b9721a09f148825ca955cf6"), "name" : "zhangSan1", "age" : 1 }
{ "_id" : ObjectId("5b9721a09f148825ca955cf7"), "name" : "zhangSan2", "age" : 2 }
{ "_id" : ObjectId("5b9721a09f148825ca955cf8"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b9721a09f148825ca955cf9"), "name" : "zhangSan4", "age" : 4 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfa"), "name" : "zhangSan5", "age" : 5 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfb"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfc"), "name" : "zhangSan7", "age" : 7 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfd"), "name" : "zhangSan8", "age" : 8 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfe"), "name" : "zhangSan9", "age" : 9 }
{ "_id" : ObjectId("5b9721a09f148825ca955cff"), "name" : "zhangSan10", "age" : 10 }
>

还可以使用 db.集合名称.save({}) 向集合中添加文档,save 与 insert 不同之处为:使用 insert 如果插入的文档中 _id 已经存在,则不能插入,类似于关系型数据库中的主键冲突

save 方法如果插入的文档中_id 已经存在,则执行更新。

> db.c1.save({name:"zhangSan",age:23})
WriteResult({ "nInserted" : 1 })
> db.c1.find()
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan", "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan", "age" : "25" }
{ "_id" : ObjectId("5b9721a09f148825ca955cf6"), "name" : "zhangSan1", "age" : 1 }
{ "_id" : ObjectId("5b9721a09f148825ca955cf7"), "name" : "zhangSan2", "age" : 2 }
{ "_id" : ObjectId("5b9721a09f148825ca955cf8"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b9721a09f148825ca955cf9"), "name" : "zhangSan4", "age" : 4 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfa"), "name" : "zhangSan5", "age" : 5 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfb"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfc"), "name" : "zhangSan7", "age" : 7 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfd"), "name" : "zhangSan8", "age" : 8 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfe"), "name" : "zhangSan9", "age" : 9 }
{ "_id" : ObjectId("5b9721a09f148825ca955cff"), "name" : "zhangSan10", "age" : 10 }
{ "_id" : ObjectId("5b9722269f148825ca955d00"), "name" : "zhangSan", "age" : 22 }
{ "_id" : ObjectId("5b97223c9f148825ca955d01"), "name" : "zhangSan", "age" : 23 }
>

删除集合中文档

删除集合中的文档,使用命令 db.集合名称.remove({删除条件})

不加删除条件为删除集合中的所有文档,例如,db.c1.remove({}) 为删除c1集合中的所有文档,里面的 {} 要有!

db.c1.remove({name:”user1”}) 为删除 c1 集合中 name 为 user1 的文档

> db.c1.find()
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan", "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan", "age" : "25" }
{ "_id" : ObjectId("5b9721a09f148825ca955cf8"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfb"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfc"), "name" : "zhangSan7", "age" : 7 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfd"), "name" : "zhangSan8", "age" : 8 }
{ "_id" : ObjectId("5b97223c9f148825ca955d01"), "name" : "zhangSan", "age" : 23 }
> db.c1.remove({age:23})
WriteResult({ "nRemoved" : 1 })
> db.c1.remove({age:7})
WriteResult({ "nRemoved" : 1 })
> db.c1.find()
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan", "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan", "age" : "25" }
{ "_id" : ObjectId("5b9721a09f148825ca955cf8"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfb"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfd"), "name" : "zhangSan8", "age" : 8 }
> db.c1.remove({name:"zhangSan8",age:8})
WriteResult({ "nRemoved" : 1 })
> db.c1.find()
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan", "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan", "age" : "25" }
{ "_id" : ObjectId("5b9721a09f148825ca955cf8"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfb"), "name" : "zhangSan6", "age" : 6 }
> db.c1.remove({})
WriteResult({ "nRemoved" : 4 })
> db.c1.find()

查询集合中文档

查询集合中的文档,可以使用命令 db.集合名称.find({条件}),或者使用 db.集合名称.findOne()  查询第一个文档

db.集合名称.find() 表示查询所有,但是 cmd 命令行面板只会最多显示 20条数据。

> db.c1.find()
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan", "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan", "age" : "25" }
{ "_id" : ObjectId("5b9721a09f148825ca955cf8"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfb"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfd"), "name" : "zhangSan8", "age" : 8 }
> db.c1.find({name:"zhangSan"})
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan", "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan", "age" : "25" }
> db.c1.findOne()
{
        "_id" : ObjectId("5b971f8e9f148825ca955cf4"),
        "name" : "zhangSan",
        "age" : "22"
}
>

查询集合中的文档,返回某些特定的键值,如下命令行查询所示,一一进行说明:

db.c1.find():查询集合 c1 中的所有文档数据

db.c1.find({}):查询集合 c1 中的所有文档数据,等价于 db.c1.find(),{} 表示查询条件为空

db.c1.find({name:"zhangSan",age:25}):查询集合 c1 中的 name 等于 zhangSan,且 age 等于 25 的文档

db.c1.find({},{age:1}):表示查询集合 c1 中的所有文档数据,但只返回 age 字段(1表示 true)

db.c1.find({},{age:0}):表示查询集合 c1 中的所有文档数据,但只返回除 age 字段以外的字段(0表示 tfalse)

db.c1.find({name:"zhangSan"},{age:1}):表示查询集合 c1 中的 name 为 zhangSan 的文档数据,且只返回 age 字段

db.c1.find({name:"zhangSan"},{name:1,age:1}):表示查询集合 c1 中的 name 为 zhangSan 的文档数据,返回 name、age 字段

第一个 {} 表示查询条件,第二个 {} 表示返回的字段

> db.c1.find()
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan", "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan", "age" : "25" }
{ "_id" : ObjectId("5b9721a09f148825ca955cf8"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfb"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfd"), "name" : "zhangSan8", "age" : 8 }
> db.c1.find({})
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan", "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan", "age" : "25" }
{ "_id" : ObjectId("5b9721a09f148825ca955cf8"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfb"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfd"), "name" : "zhangSan8", "age" : 8 }
> db.c1.find({},{age:1})
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "age" : "25" }
{ "_id" : ObjectId("5b9721a09f148825ca955cf8"), "age" : 3 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfb"), "age" : 6 }
{ "_id" : ObjectId("5b9721a09f148825ca955cfd"), "age" : 8 }
> db.c1.find({},{age:0})
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan" }
{ "_id" : ObjectId("5b9721a09f148825ca955cf8"), "name" : "zhangSan3" }
{ "_id" : ObjectId("5b9721a09f148825ca955cfb"), "name" : "zhangSan6" }
{ "_id" : ObjectId("5b9721a09f148825ca955cfd"), "name" : "zhangSan8" }
> db.c1.find({name:"zhangSan"},{age:1})
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "age" : "25" }
> db.c1.find({name:"zhangSan"},{name:1,age:1})
{ "_id" : ObjectId("5b971f8e9f148825ca955cf4"), "name" : "zhangSan", "age" : "22" }
{ "_id" : ObjectId("5b971f9e9f148825ca955cf5"), "name" : "zhangSan", "age" : "25" }
>

查询集合中的文档 ,使用条件表达式 (<, <=, >, >=,!=)

//大于: field > value

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}});

> db.c1.find()
{ "_id" : ObjectId("5b972a719f148825ca955d02"), "name" : "zhangSan0", "age" : 0 }
{ "_id" : ObjectId("5b972a719f148825ca955d03"), "name" : "zhangSan1", "age" : 1 }
{ "_id" : ObjectId("5b972a719f148825ca955d04"), "name" : "zhangSan2", "age" : 2 }
{ "_id" : ObjectId("5b972a719f148825ca955d05"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b972a719f148825ca955d06"), "name" : "zhangSan4", "age" : 4 }
{ "_id" : ObjectId("5b972a719f148825ca955d07"), "name" : "zhangSan5", "age" : 5 }
{ "_id" : ObjectId("5b972a719f148825ca955d08"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b972a719f148825ca955d09"), "name" : "zhangSan7", "age" : 7 }
{ "_id" : ObjectId("5b972a719f148825ca955d0a"), "name" : "zhangSan8", "age" : 8 }
{ "_id" : ObjectId("5b972a719f148825ca955d0b"), "name" : "zhangSan9", "age" : 9 }
{ "_id" : ObjectId("5b972a719f148825ca955d0c"), "name" : "zhangSan10", "age" : 10 }
> db.c1.find({age:{$gt:5}})
{ "_id" : ObjectId("5b972a719f148825ca955d08"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b972a719f148825ca955d09"), "name" : "zhangSan7", "age" : 7 }
{ "_id" : ObjectId("5b972a719f148825ca955d0a"), "name" : "zhangSan8", "age" : 8 }
{ "_id" : ObjectId("5b972a719f148825ca955d0b"), "name" : "zhangSan9", "age" : 9 }
{ "_id" : ObjectId("5b972a719f148825ca955d0c"), "name" : "zhangSan10", "age" : 10 }
> db.c1.find({age:{$lt:5}})
{ "_id" : ObjectId("5b972a719f148825ca955d02"), "name" : "zhangSan0", "age" : 0 }
{ "_id" : ObjectId("5b972a719f148825ca955d03"), "name" : "zhangSan1", "age" : 1 }
{ "_id" : ObjectId("5b972a719f148825ca955d04"), "name" : "zhangSan2", "age" : 2 }
{ "_id" : ObjectId("5b972a719f148825ca955d05"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b972a719f148825ca955d06"), "name" : "zhangSan4", "age" : 4 }
> db.c1.find({age:{$gte:5}})
{ "_id" : ObjectId("5b972a719f148825ca955d07"), "name" : "zhangSan5", "age" : 5 }
{ "_id" : ObjectId("5b972a719f148825ca955d08"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b972a719f148825ca955d09"), "name" : "zhangSan7", "age" : 7 }
{ "_id" : ObjectId("5b972a719f148825ca955d0a"), "name" : "zhangSan8", "age" : 8 }
{ "_id" : ObjectId("5b972a719f148825ca955d0b"), "name" : "zhangSan9", "age" : 9 }
{ "_id" : ObjectId("5b972a719f148825ca955d0c"), "name" : "zhangSan10", "age" : 10 }
> db.c1.find({age:{$lte:5}})
{ "_id" : ObjectId("5b972a719f148825ca955d02"), "name" : "zhangSan0", "age" : 0 }
{ "_id" : ObjectId("5b972a719f148825ca955d03"), "name" : "zhangSan1", "age" : 1 }
{ "_id" : ObjectId("5b972a719f148825ca955d04"), "name" : "zhangSan2", "age" : 2 }
{ "_id" : ObjectId("5b972a719f148825ca955d05"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b972a719f148825ca955d06"), "name" : "zhangSan4", "age" : 4 }
{ "_id" : ObjectId("5b972a719f148825ca955d07"), "name" : "zhangSan5", "age" : 5 }
> db.c1.find({age:{$ne:5},name:{$ne:"zhangSan6"}})
{ "_id" : ObjectId("5b972a719f148825ca955d02"), "name" : "zhangSan0", "age" : 0 }
{ "_id" : ObjectId("5b972a719f148825ca955d03"), "name" : "zhangSan1", "age" : 1 }
{ "_id" : ObjectId("5b972a719f148825ca955d04"), "name" : "zhangSan2", "age" : 2 }
{ "_id" : ObjectId("5b972a719f148825ca955d05"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b972a719f148825ca955d06"), "name" : "zhangSan4", "age" : 4 }
{ "_id" : ObjectId("5b972a719f148825ca955d09"), "name" : "zhangSan7", "age" : 7 }
{ "_id" : ObjectId("5b972a719f148825ca955d0a"), "name" : "zhangSan8", "age" : 8 }
{ "_id" : ObjectId("5b972a719f148825ca955d0b"), "name" : "zhangSan9", "age" : 9 }
{ "_id" : ObjectId("5b972a719f148825ca955d0c"), "name" : "zhangSan10", "age" : 10 }

查询集合中的文档 ,统计(count)、排序(sort)、分页(skip、limit):skip 为起始索引位置,limit 为读取的条数,类似 mysql 的 limit(X,Y) 方法,索引从0开始。

db.customer.count();——————统计集合中所有的文档个数

db.customer.find().count();——————统计集合中所有的文档个数

db.customer.find({age:{$gt:5}}).count();————统计集合中 age 大于 5 的文档个数

db.customer.find().sort({age:1}); ————查询集合中所有文档,且以 从小到大的升序排序,-1 表示 降序,默认为升序

db.customer.find().skip(2).limit(3);————查询集合中的数据,从第3条开始,连续读3条,索引从0开始。

db.customer.find().sort({age:-1}).skip(2).limit(3);——倒序查询集合中的文档,从第3条开始,连续读3条,索引从0开始。

db.customer.find().sort({age:-1}).skip(2).limit(3).count();——虽然有分页条件,但此时 count 统计的大小仍然是 find 的条件

db.customer.find().sort({age:-1}).skip(2).limit(3).count(0);——与上面一条同理

db.customer.find().sort({age:-1}).skip(2).limit(3).count(1);——此时统计的是分页后条件的大小

> db.c1.find()
{ "_id" : ObjectId("5b972a719f148825ca955d02"), "name" : "zhangSan0", "age" : 0 }
{ "_id" : ObjectId("5b972a719f148825ca955d03"), "name" : "zhangSan1", "age" : 1 }
{ "_id" : ObjectId("5b972a719f148825ca955d04"), "name" : "zhangSan2", "age" : 2 }
{ "_id" : ObjectId("5b972a719f148825ca955d05"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b972a719f148825ca955d06"), "name" : "zhangSan4", "age" : 4 }
{ "_id" : ObjectId("5b972a719f148825ca955d07"), "name" : "zhangSan5", "age" : 5 }
{ "_id" : ObjectId("5b972a719f148825ca955d08"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b972a719f148825ca955d09"), "name" : "zhangSan7", "age" : 7 }
{ "_id" : ObjectId("5b972a719f148825ca955d0a"), "name" : "zhangSan8", "age" : 8 }
{ "_id" : ObjectId("5b972a719f148825ca955d0b"), "name" : "zhangSan9", "age" : 9 }
{ "_id" : ObjectId("5b972a719f148825ca955d0c"), "name" : "zhangSan10", "age" : 10 }
> db.c1.count()
11
> db.c1.find().count()
11
> db.c1.find({}).count()
11
> db.c1.find({age:{$gt:5}}).count()
5
> db.c1.find({age:{$gt:5}}).sort({age:-1})
{ "_id" : ObjectId("5b972a719f148825ca955d0c"), "name" : "zhangSan10", "age" : 10 }
{ "_id" : ObjectId("5b972a719f148825ca955d0b"), "name" : "zhangSan9", "age" : 9 }
{ "_id" : ObjectId("5b972a719f148825ca955d0a"), "name" : "zhangSan8", "age" : 8 }
{ "_id" : ObjectId("5b972a719f148825ca955d09"), "name" : "zhangSan7", "age" : 7 }
{ "_id" : ObjectId("5b972a719f148825ca955d08"), "name" : "zhangSan6", "age" : 6 }
> db.c1.find({age:{$gt:5}}).sort({age:1})
{ "_id" : ObjectId("5b972a719f148825ca955d08"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b972a719f148825ca955d09"), "name" : "zhangSan7", "age" : 7 }
{ "_id" : ObjectId("5b972a719f148825ca955d0a"), "name" : "zhangSan8", "age" : 8 }
{ "_id" : ObjectId("5b972a719f148825ca955d0b"), "name" : "zhangSan9", "age" : 9 }
{ "_id" : ObjectId("5b972a719f148825ca955d0c"), "name" : "zhangSan10", "age" : 10 }
> db.c1.find().skip(2).limit(5)
{ "_id" : ObjectId("5b972a719f148825ca955d04"), "name" : "zhangSan2", "age" : 2 }
{ "_id" : ObjectId("5b972a719f148825ca955d05"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b972a719f148825ca955d06"), "name" : "zhangSan4", "age" : 4 }
{ "_id" : ObjectId("5b972a719f148825ca955d07"), "name" : "zhangSan5", "age" : 5 }
{ "_id" : ObjectId("5b972a719f148825ca955d08"), "name" : "zhangSan6", "age" : 6 }
> db.c1.find().sort({age:-1}).skip(2).limit(5)
{ "_id" : ObjectId("5b972a719f148825ca955d0a"), "name" : "zhangSan8", "age" : 8 }
{ "_id" : ObjectId("5b972a719f148825ca955d09"), "name" : "zhangSan7", "age" : 7 }
{ "_id" : ObjectId("5b972a719f148825ca955d08"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b972a719f148825ca955d07"), "name" : "zhangSan5", "age" : 5 }
{ "_id" : ObjectId("5b972a719f148825ca955d06"), "name" : "zhangSan4", "age" : 4 }
> db.c1.find().sort({age:-1}).skip(2).limit(5).count()
11
> db.c1.find().sort({age:-1}).skip(2).limit(5).count(0)
11
> db.c1.find().sort({age:-1}).skip(2).limit(5).count(1)
5
>

查询集合中的文档 ,$all:主要用来查询数组中的包含关系,查询条件中只要有一个不包含就不返回,如下所示:

db.c1.find({price:{$all:[1,5]}}):price 数组中同时含有元素 "1"、"5" 的是 zhangSan 和 wangWu

db.c1.find({price:{$all:[2,3,4]}}):price 数组中同时含有元素 "2"、"3"、"4" 的是 wangWu

> db.c1.find()
{ "_id" : ObjectId("5b973a559f148825ca955d0d"), "name" : "zhangSan", "price" : [ 1, 3, 5, 7, 9 ] }
{ "_id" : ObjectId("5b973a699f148825ca955d0e"), "name" : "liSi", "price" : [ 2, 4, 6, 8, 10 ] }
{ "_id" : ObjectId("5b973a7e9f148825ca955d0f"), "name" : "wangWu", "price" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }
> db.c1.find({price:{$all:[1,5]}})
{ "_id" : ObjectId("5b973a559f148825ca955d0d"), "name" : "zhangSan", "price" : [ 1, 3, 5, 7, 9 ] }
{ "_id" : ObjectId("5b973a7e9f148825ca955d0f"), "name" : "wangWu", "price" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }
> db.c1.find({price:{$all:[2,3,4]}})
{ "_id" : ObjectId("5b973a7e9f148825ca955d0f"), "name" : "wangWu", "price" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }
>

查询集合中的文档 ,$in:类似于关系型数据库中的 IN,如下所示:

db.c1.find({price:{$in:[2,3,4]}}):查询集合中 price 字段包含有 2、3、4 其中任意一个元素的文档

db.c1.find({price:{$in:[2]}}):查询集合中 price 字段包含有元素 2 的文档

> db.c1.find()
{ "_id" : ObjectId("5b973a559f148825ca955d0d"), "name" : "zhangSan", "price" : [ 1, 3, 5, 7, 9 ] }
{ "_id" : ObjectId("5b973a699f148825ca955d0e"), "name" : "liSi", "price" : [ 2, 4, 6, 8, 10 ] }
{ "_id" : ObjectId("5b973a7e9f148825ca955d0f"), "name" : "wangWu", "price" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }
> db.c1.find({price:{$in:[2,3,4]}})
{ "_id" : ObjectId("5b973a559f148825ca955d0d"), "name" : "zhangSan", "price" : [ 1, 3, 5, 7, 9 ] }
{ "_id" : ObjectId("5b973a699f148825ca955d0e"), "name" : "liSi", "price" : [ 2, 4, 6, 8, 10 ] }
{ "_id" : ObjectId("5b973a7e9f148825ca955d0f"), "name" : "wangWu", "price" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }
> db.c1.find({price:{$in:[2]}})
{ "_id" : ObjectId("5b973a699f148825ca955d0e"), "name" : "liSi", "price" : [ 2, 4, 6, 8, 10 ] }
{ "_id" : ObjectId("5b973a7e9f148825ca955d0f"), "name" : "wangWu", "price" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }
>

查询集合中的文档 ,$nin:与$in相反

 db.c1.find({price:{$nin:[4]}}):查询集合中 price 字段包不包含元素 4 的文档

> db.c1.find()
{ "_id" : ObjectId("5b973a559f148825ca955d0d"), "name" : "zhangSan", "price" : [ 1, 3, 5, 7, 9 ] }
{ "_id" : ObjectId("5b973a699f148825ca955d0e"), "name" : "liSi", "price" : [ 2, 4, 6, 8, 10 ] }
{ "_id" : ObjectId("5b973a7e9f148825ca955d0f"), "name" : "wangWu", "price" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }
> db.c1.find({price:{$nin:[4]}})
{ "_id" : ObjectId("5b973a559f148825ca955d0d"), "name" : "zhangSan", "price" : [ 1, 3, 5, 7, 9 ] }
>

查询集合中的文档 ,$or:相当于关系型数据库中的 OR,表示或者的关系,例如查询 name 为 user2 或者 age 为 3 的文档,命令为:db.customer.find({$or:[{name:”user2”},{age:3}]})

db.c1.find({$or:[{name:"liSi"},{name:"wangWu"}]}):查询 c1 集合中 name 等于 liSi 或者 name 等于 wangWu 等文档。

> db.c1.find()
{ "_id" : ObjectId("5b973a559f148825ca955d0d"), "name" : "zhangSan", "price" : [ 1, 3, 5, 7, 9 ] }
{ "_id" : ObjectId("5b973a699f148825ca955d0e"), "name" : "liSi", "price" : [ 2, 4, 6, 8, 10 ] }
{ "_id" : ObjectId("5b973a7e9f148825ca955d0f"), "name" : "wangWu", "price" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }
> db.c1.find({$or:[{name:"liSi"},{name:"wangWu"}]})
{ "_id" : ObjectId("5b973a699f148825ca955d0e"), "name" : "liSi", "price" : [ 2, 4, 6, 8, 10 ] }
{ "_id" : ObjectId("5b973a7e9f148825ca955d0f"), "name" : "wangWu", "price" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }
>

查询集合中的文档 ,$nor:表示根据条件过滤掉某些数据,例如查询 name 不是 user2,age 不是 3 的文档,命令为:db.customer.find({$nor:[{name:”user2”},{age:3}]})

db.c1.find({$nor:[{name:"liSi"},{name:"wangWu"}]}):查询 c1 集合中 name 不等于 liSi ,name 不等于 wangWu 的文档。

> db.c1.find()
{ "_id" : ObjectId("5b973a559f148825ca955d0d"), "name" : "zhangSan", "price" : [ 1, 3, 5, 7, 9 ] }
{ "_id" : ObjectId("5b973a699f148825ca955d0e"), "name" : "liSi", "price" : [ 2, 4, 6, 8, 10 ] }
{ "_id" : ObjectId("5b973a7e9f148825ca955d0f"), "name" : "wangWu", "price" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }
> db.c1.find({$nor:[{name:"liSi"},{name:"wangWu"}]})
{ "_id" : ObjectId("5b973a559f148825ca955d0d"), "name" : "zhangSan", "price" : [ 1, 3, 5, 7, 9 ] }
>

查询集合中的文档 ,$exists:用于查询集合中存在某个键的文档或不存在某个键的文档。

db.c2.find({age:{$exists:1}})————查询 c2 集合中 存在 age 字段的文档

$exists:1 表示真,指存在

$exists:0 表示假,指不存在

> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 27 }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA" }
> db.c2.find({age:{$exists:1}})
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 27 }
> db.c2.find({age:{$exists:0}})
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA" }
> db.c2.find({desc:{$exists:1}})
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA" }
>

查询集合中的文档 ,mongodb 中也有 游标 的概念,类似于关系型数据库,使用方式如下:

var c2Cur = db.c2.find():获取 集合的游标,会存在于内存中,游标变量名称自定义

c2Cur.hasNext():判断后一个是否还有值,有 则返回 true,没有时返回 false

c2Cur.next():获取后一个的值,当 c2Cur.hasNext() 为 false 时,此时会报错。

> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 27 }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA" }
> var c2Cur = db.c2.find()
> c2Cur.hasNext()
true
> c2Cur.next()
{
        "_id" : ObjectId("5b9757df9f148825ca955d10"),
        "name" : "zhangSan",
        "age" : 27
}
> c2Cur.next()
{
        "_id" : ObjectId("5b9757fa9f148825ca955d11"),
        "name" : "liSi",
        "desc" : "China"
}
> c2Cur.next()
{
        "_id" : ObjectId("5b97580b9f148825ca955d12"),
        "name" : "wangWu",
        "desc" : "USA"
}
> c2Cur.hasNext()
false
> c2Cur.next()
2018-09-11T13:59:48.510+0800 E QUERY    [js] Error: error hasNext: false :
DBQuery.prototype.next@src/mongo/shell/query.js:305:1
@(shell):1:1
>

更新集合中文档

更新集合中的文档,语法:db.collection.update(criteria,objNew,upsert,multi)

参数说明:

criteria:用于设置查询条件的对象

objNew:用于设置更新内容的对象

upsert:取值为 0 或 1。1 表示如果 criteria 记录已经存在,则更新它,否则新增一个记录;0 表示如果记录已经存在,则更新它,否则不做处理。默认为 0。

multi:取值为 0 或 1,1 表示如果有多个符合条件的记录,则全部更新;0 表示如果有多个符合条件的记录,则只更新第一条。默认为 0

一般情况下后两个参数分别为0,1 ,即:db.collection.update(criteria,objNew,0,1)

db.c3.update({name:"zhangSan",{name:"zhangSan123"}):将 name 等于 zhangSan 的文档修改为 name 等于 zhangSan123,注意此时源文档的其它字段如 age 全部被覆盖清除了。

db.c3.update({name:"zhangSan123"},{name:"zhangSan456",age:26}):修改 name 等于 zhangSan123 的文档为 name 等于 zhangSan456,age 等于 26.

就是说这样直接修改时,其实就是覆盖源}文档。

> db.c3.find()
{ "_id" : ObjectId("5b9760b79f148825ca955d13"), "name" : "zhangSan", "age" : 26 }
{ "_id" : ObjectId("5b9760c39f148825ca955d14"), "name" : "liSi", "age" : 25 }
{ "_id" : ObjectId("5b9760cb9f148825ca955d15"), "name" : "wangWu", "age" : 26 }
{ "_id" : ObjectId("5b9760d29f148825ca955d16"), "name" : "zhaoLiu", "age" : 25 }
{ "_id" : ObjectId("5b9760e59f148825ca955d17"), "name" : "maQi", "age" : 27 }
> db.c3.update({name:"zhangSan"},{name:"zhangSan123"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c3.find()
{ "_id" : ObjectId("5b9760b79f148825ca955d13"), "name" : "zhangSan123" }
{ "_id" : ObjectId("5b9760c39f148825ca955d14"), "name" : "liSi", "age" : 25 }
{ "_id" : ObjectId("5b9760cb9f148825ca955d15"), "name" : "wangWu", "age" : 26 }
{ "_id" : ObjectId("5b9760d29f148825ca955d16"), "name" : "zhaoLiu", "age" : 25 }
{ "_id" : ObjectId("5b9760e59f148825ca955d17"), "name" : "maQi", "age" : 27 }
> db.c3.update({name:"zhangSan123"},{name:"zhangSan456",age:26})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c3.find()
{ "_id" : ObjectId("5b9760b79f148825ca955d13"), "name" : "zhangSan456", "age" : 26 }
{ "_id" : ObjectId("5b9760c39f148825ca955d14"), "name" : "liSi", "age" : 25 }
{ "_id" : ObjectId("5b9760cb9f148825ca955d15"), "name" : "wangWu", "age" : 26 }
{ "_id" : ObjectId("5b9760d29f148825ca955d16"), "name" : "zhaoLiu", "age" : 25 }
{ "_id" : ObjectId("5b9760e59f148825ca955d17"), "name" : "maQi", "age" : 27 }
>

如下使用 $set :用来指定一个键的值,如果这个键不存在,则创建它,其余键不会被影响。但是默认只修改匹配的第一条数据。要想修改匹配到的所有文档,则需要设置第四个参数为 1.

> db.c3.find()
{ "_id" : ObjectId("5b9760b79f148825ca955d13"), "name" : "zhangSan_Nice", "age" : 26 }
{ "_id" : ObjectId("5b9760c39f148825ca955d14"), "name" : "liSi", "age" : 25 }
{ "_id" : ObjectId("5b9760cb9f148825ca955d15"), "name" : "wangWu", "age" : 26 }
{ "_id" : ObjectId("5b9760d29f148825ca955d16"), "name" : "zhaoLiu", "age" : 25 }
{ "_id" : ObjectId("5b9760e59f148825ca955d17"), "name" : "maQi", "age" : 27 }
> db.c3.update({age:26},{$set:{name:"zhangSan_cool"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c3.find()
{ "_id" : ObjectId("5b9760b79f148825ca955d13"), "name" : "zhangSan_cool", "age" : 26 }
{ "_id" : ObjectId("5b9760c39f148825ca955d14"), "name" : "liSi", "age" : 25 }
{ "_id" : ObjectId("5b9760cb9f148825ca955d15"), "name" : "wangWu", "age" : 26 }
{ "_id" : ObjectId("5b9760d29f148825ca955d16"), "name" : "zhaoLiu", "age" : 25 }
{ "_id" : ObjectId("5b9760e59f148825ca955d17"), "name" : "maQi", "age" : 27 }

如下所示,age 等于 27 的文档本文是没有 desc 这个字段的,$set 修改之后就有了

> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 27 }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA" }
> db.c2.update({age:27},{$set:{desc:"USA"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 27, "desc" : "USA" }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA" }
>

如下所示 修改所有 desc 等于 USA 的文档的 age 字段为 66,如果文档不存在 age 字段,则会新建。

这就是第 4 个参数为 1 的效果,如果第四个参数为 0,则只会修改第一个匹配到的文档。

> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 27, "desc" : "USA" }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA" }
> db.c2.update({desc:"USA"},{$set:{age:66}},0,1)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 66, "desc" : "USA" }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA", "age" : 66 }
>

如下所示,集合中都没有 price 等于 8888.88 的文档,此时如果第三个参数为0,则不会做任何修改,如果第三个参数为 1,则会新增此文档

> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 66, "desc" : "USA" }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA", "age" : 66 }
> db.c2.update({price:8888.88},{$set:{age:77}},0,1)
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 66, "desc" : "USA" }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA", "age" : 66 }
> db.c2.update({price:8888.88},{$set:{age:77}},1,1)
WriteResult({
        "nMatched" : 0,
        "nUpserted" : 1,
        "nModified" : 0,
        "_id" : ObjectId("5b976882c8ed70d9c9af79d2")
})
> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 66, "desc" : "USA" }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA", "age" : 66 }
{ "_id" : ObjectId("5b976882c8ed70d9c9af79d2"), "price" : 8888.88, "age" : 77 }
>

更新集合中的文档,使用 $inc

db.c2.update({age:77},{$inc:{age:3}},0,1):将 age 等于 77 的所有文档的 age 字段加上 3

db.c2.update({age:66},{$inc:{age:-3}},0,1):将 age 等于 66 的所有文档的 age 字段减去 3

> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 66, "desc" : "USA" }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA", "age" : 66 }
{ "_id" : ObjectId("5b976882c8ed70d9c9af79d2"), "price" : 8888.88, "age" : 80 }
{ "_id" : ObjectId("5b976907c8ed70d9c9af79d7"), "price" : 9999.99, "age" : 80 }
> db.c2.update({age:80},{$inc:{age:3}},0,1)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.c2.update({age:66},{$inc:{age:-3}},0,1)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 63, "desc" : "USA" }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA", "age" : 63 }
{ "_id" : ObjectId("5b976882c8ed70d9c9af79d2"), "price" : 8888.88, "age" : 83 }
{ "_id" : ObjectId("5b976907c8ed70d9c9af79d7"), "price" : 9999.99, "age" : 83 }
>

更新集合中的文档,$unset :用来删除文档中的某个键,例如删除 name 为 user1 的文档中的 address 键,可以使用命令:db.c1.update({name:”user1”},{$unset:{address:1}},0,1)

db.c2.update({age:63},{$unset:{age:1}},0,1):删除所有 age 等于 63 的文档中的 age 字段

db.c2.update({name:"liSi"},{$unset:{desc:1}},0,1):

> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "age" : 63, "desc" : "USA" }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA", "age" : 63 }
{ "_id" : ObjectId("5b976882c8ed70d9c9af79d2"), "price" : 8888.88, "age" : 83 }
{ "_id" : ObjectId("5b976907c8ed70d9c9af79d7"), "price" : 9999.99, "age" : 83 }
> db.c2.update({age:63},{$unset:{age:1}},0,1)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "desc" : "USA" }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi", "desc" : "China" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA" }
{ "_id" : ObjectId("5b976882c8ed70d9c9af79d2"), "price" : 8888.88, "age" : 83 }
{ "_id" : ObjectId("5b976907c8ed70d9c9af79d7"), "price" : 9999.99, "age" : 83 }
> db.c2.update({name:"liSi"},{$unset:{desc:1}},0,1)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c2.find()
{ "_id" : ObjectId("5b9757df9f148825ca955d10"), "name" : "zhangSan", "desc" : "USA" }
{ "_id" : ObjectId("5b9757fa9f148825ca955d11"), "name" : "liSi" }
{ "_id" : ObjectId("5b97580b9f148825ca955d12"), "name" : "wangWu", "desc" : "USA" }
{ "_id" : ObjectId("5b976882c8ed70d9c9af79d2"), "price" : 8888.88, "age" : 83 }
{ "_id" : ObjectId("5b976907c8ed70d9c9af79d7"), "price" : 9999.99, "age" : 83 }
>

 


https://www.xamrdz.com/database/62a1928774.html

相关文章: