[SERVER-5571] Issue when using spatial query within filter using $and Created: 11/Apr/12  Updated: 15/Aug/12  Resolved: 20/Apr/12

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.1.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: M Schaffer Assignee: siddharth.singh@10gen.com
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7 32 bit


Operating System: ALL
Participants:

 Description   

I am getting errors when I try a spatial query within an "$and" operator.

I can run the following queries successfully:

db.LOGGER.find({ "COORDINATES" : { "$within" :

{ "$polygon" : [[22000, 60000], [22000, 63000], [23000, 63000], [23000, 60000]] }

} });
db.LOGGER.find({ "$and" : [{ "TIME" :

{ "$gte" : NumberLong("3542546522557") }

}, { "TIME" :

{ "$lt" : NumberLong("3542546575557") }

}] });

However when I add the spatial query to the "$and" I get the following error:

db.LOGGER.find({ "$and" : [{ "COORDINATES" : { "$within" :

{ "$polygon" : [[22000, 60000], [22000, 63000], [23000, 63000], [23000, 60000]] }

} },{ "TIME" :

{ "$gte" : NumberLong("3542546522557") }

}, { "TIME" :

{ "$lt" : NumberLong("3542546575557") }

}] });

error: {
"$err" : "can't find special index: 2d for: { $and: [ { COORDINATES: { $within:

{ $polygon: [ [ 22000.0, 60000.0 ], [ 22000.0, 63000.0 ], [ 23000.0, 63000.0 ], [ 23000.0, 60000.0 ] ] }

} }, { TIME:

{ $gte: 3542546522557 }

}, { TIME:

{ $lt: 3542546575557 }

} ] }",
"code" : 13038
}

I have a collection with the following format:
{
"_id" : ObjectId("4f7c7a07d2fdb8c2db481a24"),
"ID_NAME" : NumberLong(0),
"SYSTEM_REFERENCE" : "0",
"TIME" : NumberLong("3542546568559"),
"COORDINATES" :

{ "X_COORD" : NumberLong(22682), "Y_COORD" : NumberLong(62220), "Z_COORD" : NumberLong(13) }

}
There is a 2d spatial index on "COORDINATES"
Indexes are:

db.LOGGER.getIndexes()
[
{
"name" : "id",
"ns" : "TEST.LOGGER",
"key" :

{ "_id" : 1 }

,
"v" : 0
},
{
"_id" : ObjectId("4f7c7b5aa22c3db066674ba0"),
"ns" : "TEST.LOGGER",
"key" :

{ "COORDINATES" : "2d" }

,
"name" : "Coord",
"min" : 0,
"max" : 101000
}
]



 Comments   
Comment by siddharth.singh@10gen.com [ 14/Apr/12 ]

Yes, a compound index on secondary keys (in your case time field) should help you with what you are trying to do. Please see this similar discussion on google groups http://groups.google.com/group/mongodb-user/browse_thread/thread/4417b0f9f0b3de12

Comment by M Schaffer [ 14/Apr/12 ]

What is wrong with the spatial index that I have already added (see above)?
Are you suggesting to add an index for the TIME field as well?

Comment by siddharth.singh@10gen.com [ 12/Apr/12 ]

Hi,

$and is not needed to do what you want to do. You need to have a Geospartial index on you collection like it is explained here:

http://www.mongodb.org/display/DOCS/Geospatial+Indexing#GeospatialIndexing-CompoundIndexes

Generated at Thu Feb 08 03:09:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.