查询选择器

比较

$ne

匹配所有不等于指定值的值。
Matches all values that are not equal to a specified value.
{ "x.price": { $ne: 50 } } :价格不为50的商品

$gt

匹配大于指定值的值。
Matches values that are greater than a specified value.

$gte

匹配大于或等于指定值的值。
Matches values that are greater than or equal to a specified value.

$lt

匹配小于指定值的值。
Matches values that are less than a specified value.
{ "x.likes": { $gt: 10, $lt:50 } } :选择点赞数在11到49间(含)的文档

$lte

匹配小于或等于指定值的值。
Matches values that are less than or equal to a specified value.
{ "x.likes": { $gte: 11, $lte: 49 } } :同上

$in

匹配数组中指定的任何值。
Matches any of the values specified in an array.
{ "x.name": { $in: ["张三", "李四"] } } :只查找这两人的数据

$nin

不匹配数组中指定的任何值。
Matches none of the values specified in an array.
{ "x.name": { $nin: ["张三", "李四"] } } :排除这两人的数据

$expr

比较同一条数据里的两个字段,可以使用$eq、$ne、$lt、$lte、$gt、$gte,字段用$开头。
compare fields from the same document.
{ $expr: { $gt: ["$x.spent", "$x.budget"] } } :查找超预算的开销

逻辑

$and

返回满足所有子句条件的文档。
{ $and: [ { "x.price": { $ne: 1.99 } }, { "x.price": { $exists: true } } ] }:查找价格不为空且不为1.99的商品

$or

返回与任一子句条件匹配的文档。
{ $or: [ { "x.qty": { $lt: 20 } }, { "x.price": 10 } ] } :查找数量小于20或者价格为10的商品

$nor

返回所有不能匹配的文档。
{ $nor: [ { "x.price": 1.99 }, { "x.saled": true } ] } :查找价格不为1.99未售商品

$not

返回不匹配的文档,包括该字段为空的文档。
{ "x.price": { $not: { $gt: 1.99 } } }:查找价格不为1.99或价格未设置的商品

元素

$exists

匹配具有指定字段的文档。
{ "x.name": { $exists: true }, "x.age": { $exists: false} } :有姓名但无年龄的用户

$regex

选择值与指定的正则表达式匹配的文档。
{ "x.name": { $regex: "^张" } } :查找张姓用户(^表示开头)
{ "x.name": { $regex: "x$", $options: "i" } } :以【x】或【X】结尾的姓名(
$表示结尾,i忽略大小写)

$type

匹配字段是指定类型的文档。string/object/array/bool/null/number/int/decimal/binData
{ "x.sum" : { $type: "number" } :合计为数值类型的数据
{ "x.code" : { $type : [ "string" , "number" ] } } :编码为字符类型或数值类型

数组

$and

匹配包含查询中指定的所有元素的数组。
{ $and: [ { "x.tags”: "ssl" }, { "x.tags": "security" } ] } :tags数组中要有ssl和security这两个标签

$elemMatch

每个数组元素都满足指定条件的文档。
{ "x.results": { $elemMatch: { $gte: 80, $lt: 85 } } }
{ "x.addr": { $elemMatch: { "x.province": "广东省", "x.city": "深圳市" } } }

$size

匹配数组长度为指定大小的文档。
{ "x.tags": { $size: 0 } } :标签数组为空

更多查询选择器请参考官网文档

由众触低代码平台生成和驱动