Mongodb中,aggregate不支持'$buyer'==xxx这种判断吗?代码如下
发布于 8 年前 作者 JarvisQJ 3174 次浏览 来自 问答
Consume.aggregate([
			{
				 $match:{
					  $or:[
						   {
								buyer:23
						   },
						   {
								seller:23
						   }
					  ]
				 }
			},
			{
				 $project:{
					  _id:1,
					  detail:1,
					  dealTime:1,
					  buyer:1,
					  money:{$multiply:['$money','$buyer'==23?-1:+1]}
				 }
			}
	   ])
		   .exec(function (err, consumes) {
				if(err) throw err;
				console.log(consumes)
				res.send(consumes)
		   })
		   
6 回复

这个编辑的时候有缩进,提交之后怎么没缩进了

@qujinxiong 已解决,全选后加个tab键

要使用$condition进行判断

可以参考下这个mongodb $cond

@pauky 解决了,非常感谢 money:{ $cond:{ if: { $eq: [ “$buyer”, 23 ] }, then: {$multiply:[’$money’,-1]}, else: {$multiply:[’$money’,+1] }} }

@o6875461 没有$condition,有$cond

回到顶部