一、比较运算符
运算符 | 对应到mysql的运算符 |
---|---|
$gt | > |
$gte | >= |
$in | in |
$lt | < |
$lte | <= |
$ne | != |
$nin | not in |
$all | 无对应项,指数组所有单元匹配都有,用法同$in,$nin |
语法格式:
{列名:{$gt:value}}
{列名:{$ne:value}}
{列名:{$in:[value1,value2,value3....]}}
备注:
二、逻辑运算符
运算符 | 对应到mysql的运算符 |
---|---|
$or | or |
$and | and |
$not | not |
$nor | 无对应项,指”所有列举条件都不成立,则为真” |
语法格式:
{$and:[{列:{$gte:value1}},{列:{$gle:value2}}]}
{$nor:[{列:value1},{列:value2},{列:value3}....]}
三、元素运算符
名字 | 描述 |
---|---|
$exists | 某列存在则为真 |
$mod | 满足某求余条件则为真 |
$type | 数据为某类型则为真 |
某一列是否存在 {列:{$exists:1}} #1表示存在
取5倍数的列: {列:{$mod:[5,0]}} #[5,0]表示对5进行求余,余数为0
类型 {列:{$type:2}}
类型值从1到19,还有-1和127对应的数据类型请查看官方文档:
https://docs.mongodb.com/manual/reference/operator/query/type/index.html
四、js运算符(1)
名字 | 描述 |
---|---|
$where | js表达式为真则为真 |
$regex | 正则表达式匹配则为真 |
$where 和 $regex 是将所有的的bson先转化为json对象,再供js引擎查询,消耗内存比较大
适合少量数据查询,一般情况下不建议用
语法格式:
{$where:'this.列 > value1 && this.列 < value2....'}
{列:{$regex:/pattern/<options> }}
例如:
{ name: { $regex: /acme.*corp/i, $nin: [ 'acmeblahcorp' ] } }
{ name: { $regex: /acme.*corp/, $options: 'i', $nin: [ 'acmeblahcorp' ] } }
{ name: { $regex: 'acme.*corp', $options: 'i', $nin: [ 'acmeblahcorp' ] } }
五、js运算符(2)
名字 | 描述 |
---|---|
$inc | 增长 |
$rename | 重命名列 |
$setOnInsert | 当upsert时,设置字段的值 |
$set | 设置字段的新值 |
$unset | 删除指定的列 |