megas
megas

Reputation: 21791

Using $near with $and

I'm trying to build a near query with additional condition:

query = {
    $and : [
        { address : { $near : [x, y] } },
        { available: 1 }
    ]
};

db.points.find(query)

It gives me an error:

error: {
    "$err" : "can't find any special indices: 2d (needs index), 2dsphere (needs index),  for: { $and: [ { ipaddr: { $near: [ -82.49412043543862, 0.0 ] } }, { available: 1.0 } ] }",
    "code" : 13038
}

Otherwise, the query like this works fine

query = { address : { $near : [x, y] }, available : 1 }

I need to use $and to build complex query.

Can I build $near query with $and keyword?

Upvotes: 1

Views: 479

Answers (2)

Pawel
Pawel

Reputation: 657

Probably not the greatest solution but I found a way to work around this issue. What I did is split the query into 2 parts 1) query the nearest addresses and get the objects ids 2) use it in the second query using $in operator.

Upvotes: 0

roman
roman

Reputation: 117337

see this topic - https://jira.mongodb.org/browse/SERVER-4572 - looks like it's a bug and it's not fixed yet..

Upvotes: 1

Related Questions