Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit c117c4c2ea62201078f6a46b8bbed173f78746da 1 parent acc0592
@RedBeard0531 RedBeard0531 authored
View
12 db/commands/find_and_modify.cpp
@@ -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
2  db/projection.h
@@ -94,6 +94,8 @@ namespace mongo {
*/
KeyOnly* checkKey( const BSONObj& keyPattern ) const;
+ bool includeID() const { return _includeID; }
+
private:
/**
View
6 jstests/find_and_modify2.js
@@ -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});
Please sign in to comment.
Something went wrong with that request. Please try again.