summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
authorMichael Stephens <mike@mindleak.com>2009-08-15 02:19:23 +0800
committerEliot <eliot@10gen.com>2009-08-15 03:30:59 +0800
commit752a747df8243fd9a426694a5f0ab021c79e93ef (patch)
treefad83fc3b51fb20dff52399d731a14ba0280f5e3 /db
parentcdff2417cbb685beb68ed2a56904df2c9ae4cdaa (diff)
downloadmongo-752a747df8243fd9a426694a5f0ab021c79e93ef.tar.gz
don't grab extra stuff with sub-object selectors
Signed-off-by: Eliot <eliot@10gen.com>
Diffstat (limited to 'db')
-rw-r--r--db/queryutil.cpp49
-rw-r--r--db/queryutil.h4
2 files changed, 29 insertions, 24 deletions
diff --git a/db/queryutil.cpp b/db/queryutil.cpp
index ed05a083e0d..501de5f4f2d 100644
--- a/db/queryutil.cpp
+++ b/db/queryutil.cpp
@@ -222,14 +222,11 @@ namespace mongo {
while ( i.more() ){
string s = i.next().fieldName();
if ( s.find( "." ) == string::npos ){
- fields[ s ] = "";
+ fields.insert( pair<string,string>( s , "" ) );
}
else {
string sub = s.substr( 0 , s.find( "." ) );
- if ( fields[sub].size() )
- fields[sub] = ".";
- else
- fields[sub] = s.substr( sub.size() + 1 );
+ fields.insert(pair<string,string>( sub , s.substr( sub.size() + 1 ) ) );
}
}
@@ -245,7 +242,7 @@ namespace mongo {
BSONObj FieldMatcher::getSpec() const{
BSONObjBuilder b;
- for ( map<string,string>::const_iterator i=fields.begin(); i!=fields.end(); i++){
+ for ( multimap<string,string>::const_iterator i=fields.begin(); i!=fields.end(); i++ ) {
string s = i->first;
if ( i->second.size() > 0 )
s += "." + i->second;
@@ -254,39 +251,47 @@ namespace mongo {
return b.obj();
}
- BSONObj FieldMatcher::extractDotted( const string& path , const BSONObj& o ) const {
+ void FieldMatcher::extractDotted( const string& path , const BSONObj& o , BSONObjBuilder& b ) const {
string::size_type i = path.find( "." );
if ( i == string::npos ){
const BSONElement & e = o.getField( path.c_str() );
if ( e.eoo() )
- return BSONObj();
- return e.wrap();
+ return;
+ b.append(e);
}
string left = path.substr( 0 , i );
BSONElement e = o[left];
if ( e.type() != Object )
- return BSONObj();
+ return;
BSONObj sub = e.embeddedObject();
if ( sub.isEmpty() )
- return sub;
+ return;
- BSONObjBuilder b(32);
- b.append( left.c_str() , extractDotted( path.substr( i + 1 ) , sub ) );
- return b.obj();
+ BSONObjBuilder sub_b(32);
+ extractDotted( path.substr( i + 1 ) , sub , sub_b );
+ b.append( left.c_str() , sub_b.obj() );
}
void FieldMatcher::append( BSONObjBuilder& b , const BSONElement& e ) const {
- string next = fields.find( e.fieldName() )->second;
- if ( e.eoo() ){
- }
- else if ( next.size() == 0 || next == "." || e.type() != Object ){
- b.append( e );
- }
- else {
- b.append( e.fieldName() , extractDotted( next , e.embeddedObject() ) );
+ pair<multimap<string,string>::const_iterator,multimap<string,string>::const_iterator> p = fields.equal_range( e.fieldName() );
+ BSONObjBuilder sub_b(32);
+
+ for( multimap<string,string>::const_iterator i = p.first; i != p.second; ++i ) {
+ string next = i->second;
+ if ( e.eoo() ){
+ }
+ else if ( next.size() == 0 || next == "." || e.type() != Object ){
+ b.append( e );
+ return;
+ }
+ else {
+ extractDotted( next , e.embeddedObject() , sub_b );
+ }
}
+
+ b.append( e.fieldName() , sub_b.obj() );
}
} // namespace mongo
diff --git a/db/queryutil.h b/db/queryutil.h
index 35a2267a2bc..3fb5579d3fa 100644
--- a/db/queryutil.h
+++ b/db/queryutil.h
@@ -184,9 +184,9 @@ namespace mongo {
private:
- BSONObj extractDotted( const string& path , const BSONObj& o ) const ;
+ void extractDotted( const string& path , const BSONObj& o , BSONObjBuilder& b ) const ;
- map<string,string> fields; // { 'a' : 1 , 'b.c' : 1 } ==>> [ a -> '' , b -> c ]
+ multimap<string,string> fields; // { 'a' : 1 , 'b.c' : 1 } ==>> [ a -> '' , b -> c ]
};