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

后端 未结 5 1303
一生所求
一生所求 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:18

    Starting Mongo 4.0, there is a new $toDouble aggregation operator which converts from various types to double (in this case from a string):

    // { price: "300.00" }
    // { price: "4.2" }
     db.collection.find({ $expr: { $gt: [{ $toDouble: "$price" }, 30] } })
    // { price: "300.00" }
    

提交回复
热议问题