Question

I've got a geometry-based query here that is nested within an $or and combined with the $and operators.

Mongo keeps throwing the following error:

 MongoError: Can't canonicalize query: BadValue geoNear must be top-level expr

I'm using the native Mongo node driver vs 1.4.3. I see a similar bug posted here.

Is this a case of me building the query wrong, or should I file a bug with Mongo?

{
    "$or": [
        {
            "$and": [
                {
                    "startDate": {
                        "$gt": "2013-12-27T08:00:00.000Z"
                    }
                },
                {
                    "startDate": {
                        "$lt": "2013-02-08T08:00:00.000Z"
                    }
                },
                {
                    "loc": {
                        "$near": {
                            "$geometry": {
                                "type": "Point",
                                "coordinates": [
                                    123.3423,
                                    22.2131
                                ]
                            },
                            "$maxDistance": 4
                        }
                    }
                }
            ]
        },
        {
            "$and": [
                {
                    "startDate": {
                        "$gt": "2013-12-27T08:00:00.000Z"
                    }
                },
                {
                    "startDate": {
                        "$lt": "2013-02-08T08:00:00.000Z"
                    }
                }
            ]
        }
    ]
}
Was it helpful?

Solution

I believe this is a restriction of $or and geo queries in version 2.6:

$or and GeoSpatial Queries

Changed in version 2.6.

$or supports geospatial clauses with the following exception for the near clause (near clause includes $nearSphere and $near). $or cannot contain a near clause with any other clause.

http://docs.mongodb.org/manual/reference/operator/query/or/

In other words, not a bug but a feature

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top