Skip to content

Commit 5ab6bde

Browse files
committed
Add findOneAndUpdate to MongoCollection.
1 parent b41010e commit 5ab6bde

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/Adapters/Storage/Mongo/MongoCollection.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ export default class MongoCollection {
4343
.toArray();
4444
}
4545

46+
// Atomically finds and updates an object based on query.
47+
// The result is the promise with an object that was in the database !AFTER! changes.
48+
// Postgres Note: Translates directly to `UPDATE * SET * ... RETURNING *`, which will return data after the change is done.
49+
findOneAndUpdate(query, update) {
50+
// arguments: query, sort, update, options(optional)
51+
// Setting `new` option to true makes it return the after document, not the before one.
52+
return this._mongoCollection.findAndModify(query, [], update, { new: true }).then(document => {
53+
// Value is the object where mongo returns multiple fields.
54+
return document.value;
55+
})
56+
}
57+
4658
// Atomically find and delete an object based on query.
4759
// The result is the promise with an object that was in the database before deleting.
4860
// Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done.

0 commit comments

Comments
 (0)