nuvio
nuvio

Reputation: 2625

MongoDB outside range query

I am trying to query MongoDB to obtain something like:

"get persons with age not in the range [30,40]"

I am doing:

db.persons.find({'age' : {$nin : [{$lt : 30},{$gt : 40}]}})

which is not working for me. I know that I could do something like people with age<30 AND people with age>40 but I was wondering if I can use the "not in" operator... thanks

Upvotes: 0

Views: 140

Answers (2)

ronasta
ronasta

Reputation: 2569

$in / $nin are operators used for querying for discrete values in a list and can not be used for range searches.

In your example, the query with $nin would have to be

db.persons.find({age:{$nin:[30,31,32,33,34,35,36,37,38,39,40]}})

which is not at all practical and, furthermore, would not make use of an index:

db.persons.ensureIndex({age:1})
db.persons.find({age:{$nin:[30,31,32,33,34,35,36,37,38,39,40]}}).explain()
    {
        "cursor" : "BasicCursor",
        "isMultiKey" : false,
        "n" : 1,
        "nscannedObjects" : 1,
        "nscanned" : 1,
        "nscannedObjectsAllPlans" : 1,
        "nscannedAllPlans" : 1,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 0,
        "indexBounds" : {

        },
        "server" : "Aspire-5750:27017"
    }

Sgoettschkes' answer above is correct and would use the index:

db.persons.find({$or: [{'age': {$lt: 30}},{'age': {$gt : 40}}]}).explain()
{
    "clauses" : [
        {
            "cursor" : "BtreeCursor age_1",
            "isMultiKey" : false,
            "n" : 0,
            "nscannedObjects" : 0,
            "nscanned" : 0,
            "nscannedObjectsAllPlans" : 0,
            "nscannedAllPlans" : 0,
            "scanAndOrder" : false,
            "indexOnly" : false,
            "nYields" : 0,
            "nChunkSkips" : 0,
            "millis" : 12,
            "indexBounds" : {
                "age" : [
                    [
                        -1.7976931348623157e+308,
                        30
                    ]
                ]
            }
        },
        {
            "cursor" : "BtreeCursor age_1",
            "isMultiKey" : false,
            "n" : 1,
            "nscannedObjects" : 1,
            "nscanned" : 1,
            "nscannedObjectsAllPlans" : 1,
            "nscannedAllPlans" : 1,
            "scanAndOrder" : false,
            "indexOnly" : false,
            "nYields" : 0,
            "nChunkSkips" : 0,
            "millis" : 0,
            "indexBounds" : {
                "age" : [
                    [
                        40,
                        1.7976931348623157e+308
                    ]
                ]
            }
        }
    ],
    "n" : 1,
    "nscannedObjects" : 1,
    "nscanned" : 1,
    "nscannedObjectsAllPlans" : 1,
    "nscannedAllPlans" : 1,
    "millis" : 12,
    "server" : "Aspire-5750:27017"
}

For more information on querying effectively, see http://docs.mongodb.org/manual/core/read-operations/

Upvotes: 1

Sgoettschkes
Sgoettschkes

Reputation: 13189

What about using the OR conjunction like this:

db.persons.find($or: [{'age': {$lt: 30}},{'age': {$gt : 40}}])

Upvotes: 2

Related Questions