CNode

mongodb 这个功能都没有怎么混

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

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

查看回复

回复 (8)

C
CoderIvan#1·8 年前

google

stackoverflow

google都不会,怎么混

Y
yuezm#2·8 年前

使用$where可以实现

M
mrlong#4·8 年前
引用 yuezm使用$where可以实现

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

M
mrlong#5·8 年前
引用 manyuewuxinhttps://docs.mongodb.com/manual/reference/operator/query/

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

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

@mrlong 可以啊,这样写

db.demos.find(
    {
        $where:function(){
            return this.A < this.B
        }
    }
)
M
manyuewuxin#7·8 年前
引用 mrlong@manyuewuxin 只能是 A: {$gt : 23}, 不能写 A:{$gt:B}

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

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

M
mrlong#8·8 年前
参与回复
登录后即可参与回复。登录