[{
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
Y
使用$where可以实现
M
引用 yuezm使用$where可以实现
@yuezm 只能是 A: {$gt : 23}, 不能写 A:{$gt:B}
M
引用 manyuewuxinhttps://docs.mongodb.com/manual/reference/operator/query/
@manyuewuxin 只能是 A: {$gt : 23}, 不能写 A:{$gt:B}
Y
引用 mrlong@yuezm 只能是 A: {$gt : 23}, 不能写 A:{$gt:B}
@mrlong 可以啊,这样写
db.demos.find(
{
$where:function(){
return this.A < this.B
}
}
)
M
引用 mrlong@manyuewuxin 只能是 A: {$gt : 23}, 不能写 A:{$gt:B}
@mrlong collections.aggregate([{$match: { $expr: { $gt: [ "$B", "$A" ] } }}]).toArray();
这是原生驱动库写法,mongoose我没用不清楚。
M
引用 manyuewuxin@mrlong collections.aggregate([{$match: { $expr: { $gt: [ "$B", "$A" ] } }}]).toArray(); 这是原生驱动库...
@manyuewuxin 3.6 才支持$expr 我是3.2版本,怎么办???
参与回复
登录后即可参与回复。登录