跳到主要内容
CNode

mongodb 这个功能都没有怎么混

问答
Mmrlong发布于 8 年前最后回复 8 年前
842710
[{
     name: 'xxxx',
	 A:12,
	 B:23
},
{
  name:'xxxxx',
  A:10,
  B:8
}
]

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

查看回复

回复 (8)

M
manyuewuxin#78 年前
引用 mrlong@manyuewuxin 只能是 A: {$gt : 23}, 不能写 A:{$gt:B}

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

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

Y
yuezm#68 年前
引用 mrlong@yuezm 只能是 A: {$gt : 23}, 不能写 A:{$gt:B}

@mrlong 可以啊,这样写

db.demos.find(
    {
        $where:function(){
            return this.A < this.B
        }
    }
)
M
mrlong#58 年前
引用 manyuewuxinhttps://docs.mongodb.com/manual/reference/operator/query/

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

M
mrlong#48 年前
引用 yuezm使用$where可以实现

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

Y
yuezm#28 年前

使用$where可以实现

C
CoderIvan#18 年前

google

stackoverflow

google都不会,怎么混

参与回复

登录后即可参与回复。登录