Permalink
Browse files

Support explicit exclusion of _id. SERVER-822

  • Loading branch information...
1 parent 663c3a7 commit b89833d549f5f30aa6e17bb025ef1b50132aa54c @RedBeard0531 RedBeard0531 committed May 1, 2010
Showing with 35 additions and 15 deletions.
  1. +14 −11 db/queryutil.cpp
  2. +3 −1 db/queryutil.h
  3. +2 −3 db/scanandorder.h
  4. +16 −0 jstests/fm4.js
View
@@ -698,19 +698,22 @@ namespace mongo {
uassert(13097, string("Unsupported projection option: ") + obj.firstElement().fieldName(), false);
}
- continue;
- }
+ } else if (!strcmp(e.fieldName(), "_id") && !e.trueValue()){
+ _includeID = false;
- add (e.fieldName(), e.trueValue());
+ } else {
- // validate input
- if (true_false == -1){
- true_false = e.trueValue();
- _include = !e.trueValue();
- }
- else{
- uassert( 10053 , "You cannot currently mix including and excluding fields. Contact us if this is an issue." ,
- (bool)true_false == e.trueValue() );
+ add (e.fieldName(), e.trueValue());
+
+ // validate input
+ if (true_false == -1){
+ true_false = e.trueValue();
+ _include = !e.trueValue();
+ }
+ else{
+ uassert( 10053 , "You cannot currently mix including and excluding fields. Contact us if this is an issue." ,
+ (bool)true_false == e.trueValue() );
+ }
}
}
}
View
@@ -196,10 +196,10 @@ namespace mongo {
*/
class FieldMatcher {
public:
-
FieldMatcher()
: _include(true)
, _special(false)
+ , _includeID(true)
, _skip(0)
, _limit(-1)
{}
@@ -209,6 +209,7 @@ namespace mongo {
void append( BSONObjBuilder& b , const BSONElement& e ) const;
BSONObj getSpec() const;
+ bool includeID() { return _includeID; }
private:
void add( const string& field, bool include );
@@ -221,6 +222,7 @@ namespace mongo {
typedef map<string, boost::shared_ptr<FieldMatcher> > FieldMap;
FieldMap _fields;
BSONObj _source;
+ bool _includeID;
// used for $slice operator
int _skip;
View
@@ -54,14 +54,13 @@ namespace mongo {
if ( filter ) {
BSONObjBuilder b( bb );
BSONObjIterator i( js );
- bool gotId = false;
while ( i.more() ){
BSONElement e = i.next();
const char * fname = e.fieldName();
if ( strcmp( fname , "_id" ) == 0 ){
- b.append( e );
- gotId = true;
+ if (filter->includeID())
+ b.append( e );
} else {
filter->append( b , e );
}
View
@@ -0,0 +1,16 @@
+t = db.fm4
+t.drop();
+
+t.insert({_id:1, a:1, b:1});
+
+assert.eq( t.findOne({}, {_id:1}), {_id:1}, 1)
+assert.eq( t.findOne({}, {_id:0}), {a:1, b:1}, 2)
+
+assert.eq( t.findOne({}, {_id:1, a:1}), {_id:1, a:1}, 3)
+assert.eq( t.findOne({}, {_id:0, a:1}), {a:1}, 4)
+
+assert.eq( t.findOne({}, {_id:0, a:0}), {b:1}, 6)
+assert.eq( t.findOne({}, { a:0}), {_id:1, b:1}, 5)
+
+// not sure if we want to suport this since it is the same as above
+//assert.eq( t.findOne({}, {_id:1, a:0}), {_id:1, b:1}, 5)

0 comments on commit b89833d

Please sign in to comment.