Search code examples
phpmongodblaravel-5jenssegers-mongodb

Mongo $gt, $lt query not working for NumberInt


Here is my screenshot, I don't understand why sometimes value being stored as a a number and sometimes as NumberInt.

enter image description here

And When i am try this query for search

{
    "price.egglessPrice" : { '$gt' : 360, '$lt':370}
}

I always get the result including above screenshot. but this is not right ans. There is no price lies between 360 to 370.


Solution

  • The reason behind different data type of number value depends on how you are storing your value. And you have to use $elemMatch here for matching the results:

    db.collection.find({
        price: {
            $elemMatch: {egglessPrice: { $gt: 360, $lt: 370}}
        }
    });