Skip to content

Commit

Permalink
Make findAndModify work when _id is removed in projection SERVER-2626
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeard0531 committed May 31, 2011
1 parent acc0592 commit c117c4c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions db/commands/find_and_modify.cpp
Expand Up @@ -53,6 +53,13 @@ namespace mongo {
BSONObj fieldsHolder (cmdObj.getObjectField("fields")); BSONObj fieldsHolder (cmdObj.getObjectField("fields"));
const BSONObj* fields = (fieldsHolder.isEmpty() ? NULL : &fieldsHolder); const BSONObj* fields = (fieldsHolder.isEmpty() ? NULL : &fieldsHolder);


Projection projection;
if (fields) {
projection.init(fieldsHolder);
if (!projection.includeID())
fields = NULL; // do projection in post-processing
}

BSONObj out = db.findOne(ns, q, fields); BSONObj out = db.findOne(ns, q, fields);
if (out.isEmpty()) { if (out.isEmpty()) {
if (!upsert) { if (!upsert) {
Expand Down Expand Up @@ -131,6 +138,11 @@ namespace mongo {
} }
} }


if (!fieldsHolder.isEmpty() && !fields){
// we need to run projection but haven't yet
out = projection.transform(out);
}

result.append("value", out); result.append("value", out);


return true; return true;
Expand Down
2 changes: 2 additions & 0 deletions db/projection.h
Expand Up @@ -94,6 +94,8 @@ namespace mongo {
*/ */
KeyOnly* checkKey( const BSONObj& keyPattern ) const; KeyOnly* checkKey( const BSONObj& keyPattern ) const;


bool includeID() const { return _includeID; }

private: private:


/** /**
Expand Down
6 changes: 6 additions & 0 deletions jstests/find_and_modify2.js
Expand Up @@ -8,3 +8,9 @@ assert.eq(out, {_id:1, i:1});


out = t.findAndModify({update: {$inc: {i:1}}, fields: {i:0}}); out = t.findAndModify({update: {$inc: {i:1}}, fields: {i:0}});
assert.eq(out, {_id:1, j:0}); assert.eq(out, {_id:1, j:0});

out = t.findAndModify({update: {$inc: {i:1}}, fields: {_id:0, j:1}});
assert.eq(out, {j:0});

out = t.findAndModify({update: {$inc: {i:1}}, fields: {_id:0, j:1}, 'new': true});
assert.eq(out, {j:0});

0 comments on commit c117c4c

Please sign in to comment.