一、比较运算符
运算符 | 对应到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 | 删除指定的列 |
六、示例
1、mongodb批量修改数组下元素某个字段
例如下面的数据,如果想把alias为ooo的des都改成haha
{
"_id" : ObjectId("5d5275380eb072c16ac25194"),
"name" : "test",
"items" : [
{
"id" : 1,
"content" : {
"cid" : "123",
"des" : "hello",
"alias" : "ooo"
}
},
{
"id" : 2,
"content" : {
"cid" : "124",
"des" : "world",
"alias" : "ooo"
}
}
]
}
##查找操作,通过$elemMatch处理
> db.test.find({"items":{$elemMatch:{"content.alias": "ooo"}}})
{ "_id" : ObjectId("5d5275380eb072c16ac25194"), "name" : "test", "items" : [ { "id" : 1, "content" : { "cid" : "123", "des" : "hello", "alias" : "ooo" } }, { "id" : 2, "content" : { "cid" : "124", "des" : "world", "alias" : "ooo" } } ] }
##修改操作,通过$set来操作
> db.test.update({"items":{$elemMatch:{"content.alias": "ooo"}}},{$set:{"items.$.content.des":"haha"}}, {multi: true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
##一不小心加错了字段可以用删除
> db.test.update({"items":{$elemMatch:{"content.alias": "ooo"}}},{$unset:{"xxxx":""}},false,true)
备注:
更多elemMatch操作请参考https://docs.mongodb.com/manual/reference/operator/query/elemMatch/
2、mongo 修改字典中某个字段值
例如:将详情detail中的saleChannel值为[ “OFFLINE” ]改成 [ “ONLINE”,“OFFLINE” ]
{ "_id" : NumberLong(695), "detail" : { "saleChannel" : [ "ONLINE","OFFLINE" ], "productType" : "ENTITY", "commissionType" : "NONE", "ownership" : "COMPANY" }, "name" : "欧舒丹护手霜"}
{ "_id" : NumberLong(694), "detail" : { "commissionType" : "NONE", "productType" : "ENTITY", "saleChannel" : [ "OFFLINE" ], "ownership" : "COMPANY" }, "name" : "咪咪虾条"}
{ "_id" : NumberLong(696), "detail" : { "commissionType" : "NONE", "productType" : "ENTITY", "saleChannel" : [ "OFFLINE" ], "ownership" : "COMPANY" }, "name" : "维维尼奥"}
##查找,通过 . 匹配字典中的指定字段
> db.test.find({"detail.saleChannel":["OFFLINE"]})
{ "_id" : NumberLong(694), "detail" : { "commissionType" : "NONE", "productType" : "ENTITY", "saleChannel" : [ "OFFLINE" ], "ownership" : "COMPANY" }, "name" : "咪咪虾条" }
{ "_id" : NumberLong(696), "detail" : { "commissionType" : "NONE", "productType" : "ENTITY", "saleChannel" : [ "OFFLINE" ], "ownership" : "COMPANY" }, "name" : "维维尼奥" }
##修改操作,通过$set来操作
> db.test.update({"detail.saleChannel":["OFFLINE"]},{$set:{"detail.saleChannel":["ONLINE","OFFLINE"]}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })