Skip to content

Commit

Permalink
Merge conflict resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed Feb 1, 2014
2 parents ec48f63 + a4d98a7 commit f70a86d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ db.count({}, function (err, count) {
* `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`
* `callback` (optional) signature: `err`, `numReplaced`, `newDoc`
* `numReplaced` is the number of documents replaced
* `upsert` is set to true if the upsert mode was chosen and a document was inserted
* `newDoc` is the created document if the upsert mode was chosen and a document was inserted

**Note**: you can't change a document's _id.

Expand Down Expand Up @@ -407,7 +407,7 @@ db.update({ planet: 'Mars' }, { $unset: { planet: true } }, {}, function () {

// Upserting a document
db.update({ planet: 'Pluton' }, { planet: 'Pluton', inhabited: false }, { upsert: true }, function (err, numReplaced, upsert) {
// numReplaced = 1, upsert = true
// numReplaced = 1, upsert = { _id: 'id5', planet: 'Pluton', inhabited: false }
// A new document { _id: 'id5', planet: 'Pluton', inhabited: false } has been added to the collection
});

Expand Down
6 changes: 3 additions & 3 deletions lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,9 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
if (docs.length === 1) {
return cb();
} else {
return self._insert(model.modify(query, updateQuery), function (err) {
return self._insert(model.modify(query, updateQuery), function (err, newDoc) {
if (err) { return callback(err); }
return callback(null, 1, true);
return callback(null, 1, newDoc);
});
}
});
Expand Down Expand Up @@ -546,4 +546,4 @@ Datastore.prototype.remove = function () {
};


module.exports = Datastore;
module.exports = Datastore;
13 changes: 8 additions & 5 deletions test/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -905,14 +905,15 @@ describe('Database', function () {
d.find({}, function (err, docs) {
docs.length.should.equal(0); // Default option for upsert is false

d.update({ impossible: 'db is empty anyway' }, { newDoc: true }, { upsert: true }, function (err, nr, upsert) {
d.update({ impossible: 'db is empty anyway' }, { something: "created ok" }, { upsert: true }, function (err, nr, newDoc) {
assert.isNull(err);
nr.should.equal(1);
upsert.should.equal(true);
newDoc.something.should.equal("created ok");
assert.isDefined(newDoc._id);

d.find({}, function (err, docs) {
docs.length.should.equal(1); // Default option for upsert is false
docs[0].newDoc.should.equal(true);
docs[0].something.should.equal("created ok");

done();
});
Expand Down Expand Up @@ -966,10 +967,12 @@ describe('Database', function () {
});

it('Can upsert a document even with modifiers', function (done) {
d.update({ bloup: 'blap' }, { $set: { hello: 'world' } }, { upsert: true }, function (err, nr, upsert) {
d.update({ bloup: 'blap' }, { $set: { hello: 'world' } }, { upsert: true }, function (err, nr, newDoc) {
assert.isNull(err);
nr.should.equal(1);
upsert.should.equal(true);
newDoc.bloup.should.equal('blap');
newDoc.hello.should.equal('world');
assert.isDefined(newDoc._id);

d.find({}, function (err, docs) {
docs.length.should.equal(1);
Expand Down

0 comments on commit f70a86d

Please sign in to comment.