From 02454f8697f455decd3a631abf7501303247d4f4 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 16 May 2013 14:39:11 +0200 Subject: [PATCH 1/4] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 329ca3b..2e3c86a 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,11 @@ db.findOne({ _id: 'id1' }, function (err, doc) { ``` ### Updating documents +`db.update(query, update, options, callback)` will update all documents matching `query` according to the `update` rules: +* `query` is the same kind of finding query you use with `find` and `findOne` +* `update` specifies how the documents should be modified. It is either a new document which will replace the matched ones, or a set of modifiers. The available modifiers are `$set` to change a field's value and `$inc` to increment a field's value. You cannot use mixed updates with both modes (that doesn't make sense anyway) +* `options` is an object with two possible parameters: `multi` (defaults to `false`) which allows the modification of several documents if set to true, and `upsert` (defaults to `false`) if you want to insert a new document corresponding to the `update` rules if your `query` doesn't match anything +* `callback` (optional) signature: err, numReplaced, upsert. `numReplaced` is the number of documents replaced and `upsert` is set to true if the upsert mode was chosen and a document was inserted ## Performance From a85e4bbc1afaef7f1ae47be60bd11f65bbe724ea Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 16 May 2013 15:41:00 +0300 Subject: [PATCH 2/4] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 2e3c86a..92c9029 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,11 @@ db.findOne({ _id: 'id1' }, function (err, doc) { * `options` is an object with two possible parameters: `multi` (defaults to `false`) which allows the modification of several documents if set to true, and `upsert` (defaults to `false`) if you want to insert a new document corresponding to the `update` rules if your `query` doesn't match anything * `callback` (optional) signature: err, numReplaced, upsert. `numReplaced` is the number of documents replaced and `upsert` is set to true if the upsert mode was chosen and a document was inserted +```javascript +// Let's use the same example collection as in the "finding document" part + +``` + ## Performance ### Speed From ce3256601c245abca3ba54feb5d4ebc86868902d Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 16 May 2013 16:55:59 +0200 Subject: [PATCH 3/4] Update README.md --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 92c9029..532fb03 100644 --- a/README.md +++ b/README.md @@ -85,9 +85,15 @@ db.findOne({ _id: 'id1' }, function (err, doc) { ### Updating documents `db.update(query, update, options, callback)` will update all documents matching `query` according to the `update` rules: * `query` is the same kind of finding query you use with `find` and `findOne` -* `update` specifies how the documents should be modified. It is either a new document which will replace the matched ones, or a set of modifiers. The available modifiers are `$set` to change a field's value and `$inc` to increment a field's value. You cannot use mixed updates with both modes (that doesn't make sense anyway) -* `options` is an object with two possible parameters: `multi` (defaults to `false`) which allows the modification of several documents if set to true, and `upsert` (defaults to `false`) if you want to insert a new document corresponding to the `update` rules if your `query` doesn't match anything -* `callback` (optional) signature: err, numReplaced, upsert. `numReplaced` is the number of documents replaced and `upsert` is set to true if the upsert mode was chosen and a document was inserted +* `update` specifies how the documents should be modified. It is either: + * A new document which will replace the matched ones + * A set of modifiers. The available modifiers are `$set` to change a field's value and `$inc` to increment a field's value. You cannot use mixed updates with both modes (that doesn't make sense anyway) +* `options` is an object with two possible parameters + * `multi` (defaults to `false`) which allows the modification of several documents if set to true + * `upsert` (defaults to `false`) if you want to insert a new document corresponding to the `update` rules if your `query` doesn't match anything +* `callback` (optional) signature: err, numReplaced, upsert + * `numReplaced` is the number of documents replaced + * `upsert` is set to true if the upsert mode was chosen and a document was inserted ```javascript // Let's use the same example collection as in the "finding document" part From 7fb40ee93d6b6babe03fb2c958d1ce482c9d5ff2 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 16 May 2013 18:05:09 +0300 Subject: [PATCH 4/4] Update README.md --- README.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 532fb03..bc20685 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ db.robots.loadDatabase(); The native types are String, Number, Boolean and Date. You can also use arrays and subdocuments (objects). If you specify an `_id` field, it will be used as the document's _id, otherwise nedb will generate one. -Note that the generated `_id` is a simple string, not an ObjectId. +Note that the generated `_id` is a simple string, not an ObjectId. Field names cannot begin by '$' or contain a '.'. ```javascript var document = { hello: 'world' @@ -85,9 +85,9 @@ db.findOne({ _id: 'id1' }, function (err, doc) { ### Updating documents `db.update(query, update, options, callback)` will update all documents matching `query` according to the `update` rules: * `query` is the same kind of finding query you use with `find` and `findOne` -* `update` specifies how the documents should be modified. It is either: - * A new document which will replace the matched ones - * A set of modifiers. The available modifiers are `$set` to change a field's value and `$inc` to increment a field's value. You cannot use mixed updates with both modes (that doesn't make sense anyway) +* `update` specifies how the documents should be modified. It is either a new document or a set of modifiers (you cannot use both together, it doesn't make sense!) + * A new document will replace the matched docs + * The available modifiers are `$set` to change a field's value and `$inc` to increment a field's value. The modifiers create the fields they need to modify if they don't exist, and you can apply them to subdocs (see examples below) * `options` is an object with two possible parameters * `multi` (defaults to `false`) which allows the modification of several documents if set to true * `upsert` (defaults to `false`) if you want to insert a new document corresponding to the `update` rules if your `query` doesn't match anything @@ -98,6 +98,12 @@ db.findOne({ _id: 'id1' }, function (err, doc) { ```javascript // Let's use the same example collection as in the "finding document" part +db.update({ planet: 'Jupiter' }, { planet: 'Pluton'}, {}, function (err, numReplaced) { + // numReplaced = 1 + // The doc #3 has been replaced by { _id: 'id3', planet: 'Pluton' } + // Note that the _id has not been modified +}); + ```