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"}

According to the docs, this should work:

db.products.find({price: {$gt:30.00}}).pretty()

But I get no results returned. I've also tried {price: {$gt:30}}.

What am I missing here?

It it because the prices are stored as a string rather than a number in the DB? Is there a way around this?

Solution 1

If you intend to use $gt with strings, you will have to use regex, which is not great in terms of performance. It is easier to just create a new field which holds the number value of price or change this field type to int/double. A javascript version should also work, like so:

db.products.find("this.price > 30.00")

as js will convert it to number before use. However, indexes won't work on this query.

Solution 2

$gt is an operator that can work on any type:

db.so.insert( { name: "Derick" } );
db.so.insert( { name: "Jayz" } );
db.so.find( { name: { $gt: "Fred" } } );


{ "_id" : ObjectId("51ffbe6c16473d7b84172d58"), "name" : "Jayz" }

If you want to compare against a number with $gt or $lt, then the value in your document also needs to be a number. Types in MongoDB are strict and do not auto-convert like they f.e. would do in PHP. In order to solve your issue, make sure you store the prices as numbers (floats or ints):

db.so.insert( { price: 50.40 } );
db.so.insert( { price: 29.99 } );
db.so.find( { price: { $gt: 30 } } );


{ "_id" : ObjectId("51ffbf2016473d7b84172d5b"), "price" : 50.4 }

Solution 3

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" }

Solution 4

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

Solution 5

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);
    {$set: {field: intValue}}

db.collection.find({field: {$type:2}},{field:1}).forEach(convert)