From 9193ff5d00829dccffc9572eb15a446bd4d2309e Mon Sep 17 00:00:00 2001 From: gregs Date: Wed, 6 Apr 2011 15:44:07 -0400 Subject: [PATCH] look inside indexed arrays for regexes fix for SERVER-2247 --- db/matcher.cpp | 12 +++++++++++- jstests/geo_regex0.js | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 jstests/geo_regex0.js diff --git a/db/matcher.cpp b/db/matcher.cpp index a539e8868386c..cf85819295ca1 100644 --- a/db/matcher.cpp +++ b/db/matcher.cpp @@ -820,8 +820,18 @@ namespace mongo { BSONElementSet s; if ( !constrainIndexKey_.isEmpty() ) { BSONElement e = jsobj.getFieldUsingIndexNames(rm.fieldName, constrainIndexKey_); - if ( !e.eoo() ) + + // Should only have keys nested one deep here, for geo-indices + // TODO: future indices may nest deeper? + if( e.type() == Array ){ + BSONObjIterator i( e.Obj() ); + while( i.more() ){ + s.insert( i.next() ); + } + } + else if ( !e.eoo() ) s.insert( e ); + } else { jsobj.getFieldsDotted( rm.fieldName, s ); diff --git a/jstests/geo_regex0.js b/jstests/geo_regex0.js new file mode 100644 index 0000000000000..79042b9074e05 --- /dev/null +++ b/jstests/geo_regex0.js @@ -0,0 +1,18 @@ +// From SERVER-2247 +// Tests to make sure regex works with geo indices + +t = db.regex0 +t.drop() + +t.ensureIndex( { point : '2d', words : 1 } ) +t.insert( { point : [ 1, 1 ], words : [ 'foo', 'bar' ] } ) + +regex = { words : /^f/ } +geo = { point : { $near : [ 1, 1 ] } } +both = { point : { $near : [ 1, 1 ] }, words : /^f/ } + +assert.eq(1, t.find( regex ).count() ) +assert.eq(1, t.find( geo ).count() ) +assert.eq(1, t.find( both ).count() ) + +