MongoDB $gt/$lt operators with prices stored as strings

后端 未结 5 1304
一生所求
一生所求 2020-11-29 06:27

I\'m trying to query my database for prices greater than/less than a user specified number. In my database, prices are stored like so:

{price: \"300.00\"}
         


        
5条回答
  •  野性不改
    2020-11-29 07:16

    If you have newer version of mongodb then you can do this:

    $expr: {
              $gt: [
                 { $convert: { input: '$price', to: 'decimal' } },
                 { $convert: { input: '0.0', to: 'decimal' } }
                   ]
                  }
    

    $expr operator: https://docs.mongodb.com/manual/reference/operator/query/expr/

    $convert opetator: https://docs.mongodb.com/manual/reference/operator/aggregation/convert/index.html

提交回复
热议问题