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

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

    Alternatively you can convert the values to Int, as per: http://www.quora.com/How-can-I-change-a-field-type-from-String-to-Integer-in-mongodb

    var convert = function(document){
    var intValue = parseInt(document.field, 10);
      db.collection.update(
        {_id:document._id}, 
        {$set: {field: intValue}}
      );
    }
    
    db.collection.find({field: {$type:2}},{field:1}).forEach(convert)
    

提交回复
热议问题