summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias Stearn <mathias@10gen.com>2013-04-18 08:42:40 -0400
committerMathias Stearn <redbeard0531@gmail.com>2013-04-18 19:02:42 -0400
commit022affc651140b5ad28ff5a48b5e4564e1d97317 (patch)
treebfd346364de2b2fbf94d4e406cbc213775aa3d09
parent0b48ec01cb707a485cf46e01be05b6c0aa04cd5a (diff)
downloadmongo-022affc651140b5ad28ff5a48b5e4564e1d97317.tar.gz
SERVER-9385 Make sure _id fields are extracted from modified lazy V8 objects
-rw-r--r--jstests/server9385.js16
-rw-r--r--src/mongo/scripting/engine_v8.cpp7
2 files changed, 21 insertions, 2 deletions
diff --git a/jstests/server9385.js b/jstests/server9385.js
new file mode 100644
index 00000000000..ee86891ce2a
--- /dev/null
+++ b/jstests/server9385.js
@@ -0,0 +1,16 @@
+// SERVER-9385 ensure saving a document derived from bson->js conversion doesn't lose it's _id
+t = db.server9385;
+t.drop();
+
+t.insert( { _id : 1, x : 1 } );
+x = t.findOne();
+x._id = 2;
+t.save( x );
+
+t.find().forEach( printjson );
+
+assert.eq( 2, t.find().count() );
+assert.eq( 2, t.find().itcount() );
+
+assert( t.findOne( { _id : 1 } ), "original insert missing" );
+assert( t.findOne( { _id : 2 } ), "save didn't work?" );
diff --git a/src/mongo/scripting/engine_v8.cpp b/src/mongo/scripting/engine_v8.cpp
index 484b748b975..c1e3648e528 100644
--- a/src/mongo/scripting/engine_v8.cpp
+++ b/src/mongo/scripting/engine_v8.cpp
@@ -1655,8 +1655,11 @@ namespace mongo {
}
BSONObjBuilder b;
+
+ // We special case the _id field in top-level objects and move it to the front.
+ // This matches other drivers behavior and makes finding the _id field quicker in BSON.
if (depth == 0) {
- if (o->HasRealNamedProperty(v8::String::New("_id"))) {
+ if (o->HasOwnProperty(v8::String::New("_id"))) {
v8ToMongoElement(b, "_id", o->Get(v8::String::New("_id")), 0, &originalBSON);
}
}
@@ -1667,7 +1670,7 @@ namespace mongo {
v8::Local<v8::Value> value = o->Get(name);
const string sname = toSTLString(name);
if (depth == 0 && sname == "_id")
- continue;
+ continue; // already handled above
v8ToMongoElement(b, sname, value, depth + 1, &originalBSON);
}