MongoDB查询表达式

一、比较运算符

运算符对应到mysql的运算符
$gt>
$gte>=
$inin
$lt<
$lte<=
$ne!=
$ninnot in
$all无对应项,指数组所有单元匹配都有,用法同$in,$nin
语法格式:
{列名:{$gt:value}}
{列名:{$ne:value}}
{列名:{$in:[value1,value2,value3....]}}  
备注:

二、逻辑运算符

运算符对应到mysql的运算符
$oror
$andand
$notnot
$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)

名字描述
$wherejs表达式为真则为真
$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 })
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值