Mongo field A greater than field B

后端 未结 4 983
渐次进展
渐次进展 2020-12-09 15:58

i\'m trying a simple query in Mongo which in MySQL would look like this.

select * from emails where bounceCount > sentCount;

So far I ha

4条回答
  •  北海茫月
    2020-12-09 16:48

    Everyone seems to mention $where without really knowing that it is:

    • slow
    • insecure (evaled)
    • JavaScript, not MongoDB internals
    • And, on versions prior to 2.4, single threaded and global locked

    Another method which would be a lot better for about 99% of cases is to use the aggregation framework:

    db.col.aggregate([
        {$project: {ab: {$cmp: ['$bounceCount','$sentCount']}}},
        {$match: {ab:{$gt:0}}}
    ])
    

提交回复
热议问题