摘要:本文主要学习了查询操作符和更新操作符。
环境
Windows 10 企业版 LTSC 21H2
MongoDB 6.0.21
1 查询操作符
1.1 比较查询
1.1.1 eq
等于,查询属性等于指定值的文档。
查询age属性等于25的文档:
1 | db.student.find({ age: { $eq: 25 } }); |
1.1.2 ne
不等于,查询属性不等于指定值的文档。
查询age属性不等于25的文档:
1 | db.student.find({ age: { $ne: 25 } }); |
1.1.3 gt
大于,查询属性大于指定值的文档。
查询age属性大于25的文档:
1 | db.student.find({ age: { $gt: 25 } }); |
1.1.4 lt
小于,查询属性小于指定值的文档。
查询age属性小于25的文档:
1 | db.student.find({ age: { $lt: 25 } }); |
1.1.5 gte
大于等于,查询属性大于等于指定值的文档。
查询age属性大于等于25的文档:
1 | db.student.find({ age: { $gte: 25 } }); |
1.1.6 lte
小于等于,查询属性小于等于指定值的文档。
查询age属性小于等于25的文档:
1 | db.student.find({ age: { $lte: 25 } }); |
1.1.7 in
包含,查询属性在指定范围的文档。
查询age属性在25和35的文档:
1 | db.student.find({ age: { $in: [ 25, 35 ] } }); |
1.1.8 nin
不包含,查询属性不在指定范围的文档。
查询age属性不在25和35的文档:
1 | db.student.find({ age: { $nin: [ 25, 35 ] } }); |
1.2 逻辑查询
1.2.1 and
逻辑与,查询指定条件全都满足的文档。
查询age属性等于25且sex属性为女的文档:
1 | db.student.find({ |
可以省略操作符:
1 | db.student.find({ |
1.2.2 or
逻辑或,查询指定条件部分满足的文档。
查询age属性等于25或sex属性为女的文档:
1 | db.student.find({ |
1.2.3 nor
逻辑非或,查询指定条件都不满足的文档。
查询age属性不等于25且sex属性不为女的文档:
1 | db.student.find({ |
1.2.4 not
逻辑非,查询指定条件不满足的文档。
查询age属性不等于25的文档:
1 | db.student.find({ age: { $not: { $eq: 25 } } }); |
等价于:
1 | db.student.find({ age: { $ne: 25 } }); |
1.3 元素查询
1.3.1 exists
查询指定属性是否存在的文档。
查询存在age属性的文档:
1 | db.student.find({ age: { $exists: true } }); |
1.3.2 type
查询指定属性是否符合指定类型的文档。
查询age属性类型为整数的文档:
1 | db.student.find({ age: { $type: [ "double", "int" ] } }); |
1.4 评估查询
1.4.1 expr
允许使用聚合操作符查询文档。
查询income属性大于expenditure属性的文档:
1 | db.student.find({ $expr: { $gt: [ "$income", "$expenditure" ] } }); |
1.4.2 regex
允许使用正则表达式查询文档。
查询address属性以北京市开头的文档:
1 | db.student.find({ address: { $regex: /^北京市/ } }); |
1.5 数组查询
1.5.1 all
查询属性是数组并且包含所有元素的文档。
查询tags数组中同时包含age和sex的文档:
1 | db.student.find({ tags: { $all: [ "age", "sex" ] } }); |
1.5.2 elemMatch
查询属性是数组并且满足指定条件的文档。
查询scores数组中存在大于80的文档:
1 | db.student.find({ scores: { $elemMatch: { $gt: 80 } } }); |
1.5.3 size
查询属性是数组并且符合指定大小的文档。
查询books数组大小为2的文档:
1 | db.student.find({ books: { $size: 2 } }); |
2 更新操作符
2.1 属性更新
2.1.1 rename
根据条件修改指定属性名。
查询name属性为张三的文档,将location属性名修改为address:
1 | db.student.updateOne( |
2.1.2 set
根据条件修改指定属性值。
查询name属性为张三的文档,将age属性值设置为30:
1 | db.student.updateOne( |
2.1.3 unset
根据条件删除指定属性。
查询name属性为张三的文档,将age属性删除:
1 | db.student.updateOne( |
2.1.4 inc
根据条件将属性按指定值相加。
查询name属性为张三的文档,将age属性减1:
1 | db.student.updateOne( |
2.1.5 mul
根据条件将属性按指定值相乘。
查询name属性为张三的文档,将age属性乘2:
1 | db.student.updateOne( |
2.1.6 min
根据条件设置最大值,当指定值小于属性值,更新为指定值。
查询name属性为张三的文档,将age属性的最大值更新为16:
1 | db.student.updateOne( |
2.1.7 max
根据条件设置最小值,当指定值大于属性值,更新为指定值。
查询name属性为张三的文档,将age属性的最小值更新为18:
1 | db.student.updateOne( |
2.2 数组更新
2.2.1 pop
将元素从数组的开头或末尾移除,末尾是1,开头是-1。
查询name属性为张三的文档,将元素从scores数组的末尾移除:
1 | db.student.updateOne( |
2.2.2 push
将元素添加到数组的末尾。
查询name属性为张三的文档,将元素添加到scores数组的末尾:
1 | db.student.updateOne( |
2.2.3 pull
将元素从数组中移除。
查询name属性为张三的文档,将元素从scores数组移除:
1 | db.student.updateOne( |
2.2.4 pullAll
将多个元素从数组中移除。
查询name属性为张三的文档,将多个元素从scores数组移除:
1 | db.student.updateOne( |
等价于:
1 | db.student.updateOne( |
条