查询操作符
基本比较操作符
$eq:等于
db.collection.find({
field: {
$eq: value } });
- field:要比较的字段
- value:要匹配的值
$ne:不等于
db.collection.find({
field: {
$ne: value } });
- field:要比较的字段
- value:要排除的值
$gt:大于
db.collection.find({
field: {
$gt: value } });
- field:要比较的字段
- value:大于该值
$lt:小于
db.collection.find({
field: {
$lt: value } });
- field:要比较的字段
- value:小于该值
$gte:大于等于
db.collection.find({
field: {
$gte: value } });
- field:要比较的字段
- value:大于等于该值
$lte:小于等于
db.collection.find({
field: {
$lte: value } });
- field:要比较的字段
- value:小于等于该值
逻辑操作符
$and:逻辑与
db.collection.find({
$and: [ {
field1: value1 }, {
field2: value2 } ] });
- $and:一个数组,其中包含多个查询条件,所有条件都必须满足
$or:逻辑或
db.collection.find({
$or: [ {
field1: value1 }, {
field2: value2 } ] });
- $or:一个数组,其中包含多个查询条件,只需满足其中一个条件
$not:逻辑非
db.collection.find({
field: {
$not: {
$regex: /pattern/ } } });
- $not:用于否定一个查询条件
$nor:逻辑非或
db.collection.find({
$nor: [ {
field1: value1 }, {
field2: value2 } ] });
- $nor:一个数组,其中包含多个查询条件,所有条件都必须不满足
元素操作符
$exists:检查字段是否存在
db.collection.find({
field: {
$exists: true } });
- field:要检查的字段
- true 或 false:是否检查字段的存在
$type:检查字段的数据类型
db.collection.find({
field: {
$type: "string" } });
- field:要检查的字段
- “string”:数据类型,可以是 number, object, array 等
数组操作符
$all:匹配数组中所有指定值
db.collection.find({
field: {
$all: [value1, value2] } });
- field:要检查的数组字段
- [value1, value2]:数组中必须包含的所有值
$elemMatch:匹配数组中满足指定条件的元素
db.collection.find({
field: {
$elemMatch: {
subField: value } } });
- field:要检查的数组字段
- subField:数组元素中的子字段
$size:匹配数组的大小
db.collection.find({
field: {
$size: 3 } });
- field:要检查的数组字段
- 3:数组的大小
正则表达式操作符
$regex:匹配正则表达式
db.collection.find({
field: {
$regex: /pattern/ } });
- field:要匹配的字段
- /pattern/:正则表达式模式
$options:指定正则表达式选项
db.collection.find({
field: {
$regex: /pattern/, $options: "i" } });
- i:不区分大小写
- m:多行模式
- s:单行模式
更新操作符
字段操作符
$set:设置字段的值
db.collection.updateOne({
_id: id }, {
$set: {
field: value } });
- field:要设置的字段
- value:字段的新值
$unset:删除字段
db.collection.updateOne({
_id: id }, {
$unset: {
field: "" } });
- field:要删除的字段
$inc:增加字段的值
db.collection.updateOne({
_id: id }, {
$inc: {
field: 1 } });
- field:要增加的字段
- 1:增加的值
$mul:乘以字段的值
db.collection.updateOne({
_id: id }, {
$mul: {
field: 2 } });
- field:要乘以的字段
- 2:乘法因子
$rename:重命名字段
db.collection.updateOne({
_id: id }, {
$rename: {
oldField: "newField" } });
- oldField:旧字段名称
- newField:新字段名称
数组操作符
$push:向数组中添加一个或多个元素
db.collection.updateOne({
_id: id }, {
$push: {
field: value } });
- field:要更新的数组字段
- value:要添加的元素
$addToSet:向数组中添加一个元素,如果元素不存在
db.collection.updateOne({
_id: id }, {
$addToSet: {
field: value } });
- field:要更新的数组字段
- value:要添加的元素
$pop:从数组中移除第一个或最后一个元素
db.collection.updateOne({
_id: id }, {
$pop: {
field: 1 } });
- field:要更新的数组字段
- 1:移除数组的最后一个元素,-1:移除数组的第一个元素
$pull:从数组中移除所有匹配的元素
db.collection.updateOne({
_id: id }, {
$pull: {
field: value } });
- field:要更新的数组字段
- value:要移除的元素
$pullAll:从数组中移除所有指定的元素
db.collection.updateOne({
_id: id }, {
$pullAll: {
field: [value1, value2] } });
- field:要更新的数组字段
- [value1, value2]:要移除的多个元素
$slice:限制数组的大小或获取数组的一个子集
db.collection.updateOne({
_id: id }, {
$push: {
field: {
$each: [value1, value2], $slice: 5 } } });
- field:要更新的数组字段
- $each:指定要添加的元素
- $slice:限制数组的最大长度
聚合操作符
分组操作符
$sum:计算总和
db.collection.aggregate([
{
$group: {
_id: null, total: {
$sum: "$field" } } }
]);
- total:计算字段 field 的总和
$avg:计算平均值
db.collection.aggregate([
{
$group: {
_id: null, average: {
$avg: "$field" } } }
]);
- average:计算字段 field 的平均值
$max:找出最大值
db.collection.aggregate([
{
$group: {
_id: null, maximum: {
$max: "$field" } } }
]);
- maximum:字段 field 的最大值
$min:找出最小值
db.collection.aggregate([
{
$group: {
_id: null, minimum: {
$min: "$field" } } }
]);
- minimum:字段 field 的最小值
筛选操作符
$match:筛选文档
db.collection.aggregate([
{
$match: {
field: value } }
]);
- field:要匹配的字段
- value:要匹配的值
$project:选择要包含的字段
db.collection.aggregate([
{
$project: {
field1: 1, field2: 1 } }
]);
- field1 和 field2:要包含的字段
排序和限制
$sort:排序结果
db.collection.aggregate([
{
$sort: {
field: 1 } }
]);
- field:要排序的字段,1 为升序,-1 为降序
$limit:限制结果数量
db.collection.aggregate([
{
$limit: 10 }
]);
- 10:要返回的文档数量
$skip:跳过指定数量的结果
db.collection.aggregate([
{
$skip: 5 }
]);
- 5:要跳过的文档数量