mongodb 这个功能都没有怎么混
发布于 6 年前 作者 mrlong 2878 次浏览 最后一次编辑是 5 年前 来自 问答
[{
     name: 'xxxx',
	 A:12,
	 B:23
},
{
  name:'xxxxx',
  A:10,
  B:8
}
]

我想查出 B 比 A 大 的document . 怎么写。没有,在mysql 最常用了 select * from table where B > A , 到了mongodb 就无法做到??

8 回复

google

stackoverflow

google都不会,怎么混

使用$where可以实现

@yuezm 只能是 A: {$gt : 23}, 不能写 A:{$gt:B}

@manyuewuxin 只能是 A: {$gt : 23}, 不能写 A:{$gt:B}

@mrlong 可以啊,这样写

db.demos.find(
    {
        $where:function(){
            return this.A < this.B
        }
    }
)

@mrlong collections.aggregate([{$match: { $expr: { $gt: [ “$B”, “$A” ] } }}]).toArray();

这是原生驱动库写法,mongoose我没用不清楚。

@manyuewuxin 3.6 才支持$expr 我是3.2版本,怎么办???

回到顶部