From c739ec696b382222b38e382f14f6ed9e66d90106 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Fri, 17 May 2013 15:48:01 +0200 Subject: [PATCH 1/3] Update README.md --- README.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 639bd61..163e018 100644 --- a/README.md +++ b/README.md @@ -119,11 +119,19 @@ db.update({ system: 'solar' }, { $set: { system: 'solar system' } }, { multi: tr // Field 'system' on Mars, Earth, Jupiter now has value 'solar system' }); -// Incrementing the value of a non-existing field in a subdocument -db.update({ planet: 'Mars' }, { $inc: { "data.satellites": 2 } }, {}, function () { +// Incrementing the value of a non-existing field in a subdocument by using the dot-notation +db.update({ planet: 'Mars' }, { $set: { "data.satellites": 2, "data.red": true } }, {}, function () { // Mars document now is { _id: 'id1', system: 'solar', inhabited: false - // , data: { satellites: 2 } + // , data: { satellites: 2, red: true } // } + // Not that to set fields in subdocuments, you HAVE to use dot-notation + // Using object-notation will just replace the top-level field + db.update({ planet: 'Mars' }, { $set: { date: { satellites: 3 } } }, {}, function () { + // Mars document now is { _id: 'id1', system: 'solar', inhabited: false + // , data: { satellites: 3 } + // } + // You lost the "data.red" field which is probably not the intended behavior + }); }); // Upserting a document @@ -133,6 +141,7 @@ db.update({ planet: 'Pluton' }, { planet: 'Pluton', inhabited: false }, { upsert }); // If you upsert with a modifier, the upserted doc is the query modified by the modifier +// This is simpler than it sounds :) db.update({ planet: 'Pluton' }, { $set: { inhabited: false } }, { upsert: true }, function () { // A new document { _id: 'id5', planet: 'Pluton', inhabited: false } has been added to the collection }); From 9521254b29b8b09a50cad974ed41efa4d9c331ba Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Fri, 17 May 2013 15:50:13 +0200 Subject: [PATCH 2/3] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 163e018..af16a5c 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ db.update({ system: 'solar' }, { $set: { system: 'solar system' } }, { multi: tr // Field 'system' on Mars, Earth, Jupiter now has value 'solar system' }); -// Incrementing the value of a non-existing field in a subdocument by using the dot-notation +// Setting the value of a non-existing field in a subdocument by using the dot-notation db.update({ planet: 'Mars' }, { $set: { "data.satellites": 2, "data.red": true } }, {}, function () { // Mars document now is { _id: 'id1', system: 'solar', inhabited: false // , data: { satellites: 2, red: true } @@ -142,8 +142,8 @@ db.update({ planet: 'Pluton' }, { planet: 'Pluton', inhabited: false }, { upsert // If you upsert with a modifier, the upserted doc is the query modified by the modifier // This is simpler than it sounds :) -db.update({ planet: 'Pluton' }, { $set: { inhabited: false } }, { upsert: true }, function () { - // A new document { _id: 'id5', planet: 'Pluton', inhabited: false } has been added to the collection +db.update({ planet: 'Pluton' }, { $inc: { distance: 38 } }, { upsert: true }, function () { + // A new document { _id: 'id5', planet: 'Pluton', distance: 38 } has been added to the collection }); ``` From 962c1e4ea61e5d23bdea269e6a1e3abf9cc527fc Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Fri, 17 May 2013 16:51:23 +0300 Subject: [PATCH 3/3] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index af16a5c..b1dec44 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,10 @@ db.findOne({ _id: 'id1' }, function (err, doc) { ```javascript // Let's use the same example collection as in the "finding document" part +// { _id: 'id1', planet: 'Mars', system: 'solar', inhabited: false } +// { _id: 'id2', planet: 'Earth', system: 'solar', inhabited: true } +// { _id: 'id3', planet: 'Jupiter', system: 'solar', inhabited: false } +// { _id: 'id4', planet: 'Omicron Persia 8', system: 'futurama', inhabited: true } // Replace a document by another db.update({ planet: 'Jupiter' }, { planet: 'Pluton'}, {}, function (err, numReplaced) { @@ -155,6 +159,10 @@ db.update({ planet: 'Pluton' }, { $inc: { distance: 38 } }, { upsert: true }, fu ```javascript // Let's use the same example collection as in the "finding document" part +// { _id: 'id1', planet: 'Mars', system: 'solar', inhabited: false } +// { _id: 'id2', planet: 'Earth', system: 'solar', inhabited: true } +// { _id: 'id3', planet: 'Jupiter', system: 'solar', inhabited: false } +// { _id: 'id4', planet: 'Omicron Persia 8', system: 'futurama', inhabited: true } // Remove one document from the collection // options set to {} since the default for multi is false