Skip to content

Commit 2440099

Browse files
committed
Add findOneAndUpdate to MongoCollection.
1 parent e39286d commit 2440099

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/Adapters/Storage/Mongo/MongoCollection.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,19 @@ export default class MongoCollection {
4646
count(query, { skip, limit, sort } = {}) {
4747
return this._mongoCollection.count(query, { skip, limit, sort });
4848
}
49-
49+
50+
// Atomically finds and updates an object based on query.
51+
// The result is the promise with an object that was in the database !AFTER! changes.
52+
// Postgres Note: Translates directly to `UPDATE * SET * ... RETURNING *`, which will return data after the change is done.
53+
findOneAndUpdate(query, update) {
54+
// arguments: query, sort, update, options(optional)
55+
// Setting `new` option to true makes it return the after document, not the before one.
56+
return this._mongoCollection.findAndModify(query, [], update, { new: true }).then(document => {
57+
// Value is the object where mongo returns multiple fields.
58+
return document.value;
59+
})
60+
}
61+
5062
// Atomically find and delete an object based on query.
5163
// The result is the promise with an object that was in the database before deleting.
5264
// Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done.

0 commit comments

Comments
 (0)