数据库查询时怎么比较2个字段?
发布于 7 年前 作者 tao53 12800 次浏览 来自 官方Issues

有个number字段,A,B,查询A>B的条件怎么写?A:_.gt(B)不行。

3 回复

暂不支持基于同一条记录某field的任何运算。

如果非要实现这种需求,请在写入doc的时候添加新field:

C:A>B

  1. 注意是否const _ = db.command;

  2. 注意AB数据是否都是严格的number如1而非'1'类

A > B

const $ = db.command.aggregate

db.collection(‘table2’).aggregate()

.addFields({

    matched: $.gt([’$A’, ‘$B’])

})

.match({

    matched:!0

})

.end().then(res => console.log(res))

.catch(err => console.error(err))

=================

A*B > 300

db.collection(‘table2’).aggregate()

.addFields({

    matched: $.gt([$.multiply([’$A’, ‘$B’]), 300])

})

.match({

    matched:!0

})

.end().then(res => console.log(res))

.catch(err => console.error(err))

==============

多个数或字段值相乘

$.multiply([’$A’, ‘$B’, 10,…])

https://image.wxopen.club/content_b96552dc-4f59-11ea-9e8e-001a7dda7111.png

还有很多运算api,自己每个都点一下,看看是什么意思,以后需要的时候,就知道有没有什么api可以使用了

回到顶部