Skip to content

Commit be18dae

Browse files
committed
atomic upsertWithWhere
Signed-off-by: = <mrbatista@users.noreply.github.com>
1 parent e60b2e8 commit be18dae

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
coverage
33
.idea
44
.nyc_output/
5+
.history

lib/mongodb.js

+71
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,77 @@ MongoDB.prototype.update = MongoDB.prototype.updateAll = function updateAll(
16921692
);
16931693
};
16941694

1695+
MongoDB.prototype.upsertWithWhere = function upsertWithWhere(
1696+
modelName,
1697+
where,
1698+
data,
1699+
options,
1700+
cb,
1701+
) {
1702+
const self = this;
1703+
1704+
if (self.debug) {
1705+
debug('upsertWithWhere', modelName, where, data);
1706+
}
1707+
1708+
let updateData = Object.assign({}, data);
1709+
1710+
const idValue = self.getIdValue(modelName, updateData);
1711+
const idName = self.idName(modelName);
1712+
1713+
where = self.buildWhere(modelName, where, options);
1714+
1715+
if (idValue === null || idValue === undefined) {
1716+
delete updateData[idName]; // Allow MongoDB to generate the id
1717+
} else {
1718+
const oid = self.coerceId(modelName, idValue, options); // Is it an Object ID?c
1719+
updateData._id = oid; // Set it to _id
1720+
if (idName !== '_id') {
1721+
delete updateData[idName];
1722+
}
1723+
}
1724+
1725+
updateData = self.toDatabase(modelName, updateData);
1726+
1727+
// Check for other operators and sanitize the data obj
1728+
updateData = self.parseUpdateData(modelName, updateData, options);
1729+
1730+
this.execute(
1731+
modelName,
1732+
'findOneAndUpdate',
1733+
where,
1734+
updateData,
1735+
{
1736+
upsert: true,
1737+
returnOriginal: false,
1738+
sort: [['_id', 'asc']],
1739+
},
1740+
function(err, result) {
1741+
if (err) return cb && cb(err);
1742+
1743+
if (self.debug)
1744+
debug('upsertWithWhere.callback', modelName, where, updateData, err, info);
1745+
1746+
const object = result && result.value;
1747+
self.setIdValue(modelName, object, object._id);
1748+
if (object && idName !== '_id') {
1749+
delete object._id;
1750+
}
1751+
1752+
let info;
1753+
if (result && result.lastErrorObject) {
1754+
info = {isNewInstance: !result.lastErrorObject.updatedExisting};
1755+
} else {
1756+
debug('upsertWithWhere result format not recognized: %j', result);
1757+
}
1758+
1759+
if (cb) {
1760+
cb(err, self.fromDatabase(modelName, object), info);
1761+
}
1762+
},
1763+
);
1764+
};
1765+
16951766
/**
16961767
* Disconnect from MongoDB
16971768
*/

0 commit comments

Comments
 (0)