Permalink
Browse files

Make findAndModify work when _id is removed in projection SERVER-2626

  • Loading branch information...
1 parent acc0592 commit c117c4c2ea62201078f6a46b8bbed173f78746da @RedBeard0531 RedBeard0531 committed May 31, 2011
Showing with 20 additions and 0 deletions.
  1. +12 −0 db/commands/find_and_modify.cpp
  2. +2 −0 db/projection.h
  3. +6 −0 jstests/find_and_modify2.js
@@ -53,6 +53,13 @@ namespace mongo {
BSONObj fieldsHolder (cmdObj.getObjectField("fields"));
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);
if (out.isEmpty()) {
if (!upsert) {
@@ -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);
return true;
View
@@ -94,6 +94,8 @@ namespace mongo {
*/
KeyOnly* checkKey( const BSONObj& keyPattern ) const;
+ bool includeID() const { return _includeID; }
+
private:
/**
@@ -8,3 +8,9 @@ assert.eq(out, {_id:1, i:1});
out = t.findAndModify({update: {$inc: {i:1}}, fields: {i: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.