parse-community / parse-server

Parse Server for Node.js / Express
https://parseplatform.org
Apache License 2.0
20.92k stars 4.78k forks source link

Support MongoDB's findAndModify #7819

Open mtrezza opened 2 years ago

mtrezza commented 2 years ago

New Feature / Enhancement Checklist

Current Limitation

Parse does not natively support a way to issue a query and in the same operation write to the found documents. This is a concept used to uniquely process documents in an environment with concurrent operations.

Feature / Enhancement Description

Add a method set or update to Parse.Query that if set executes Parse.Query.first as a findAndModify in MongoDB.

Example Use Case

  1. Request A is a findAndModify request for documents that are without a "being processed" flag, which returns one document which will be flagged as being processed immediately.
  2. Request B is the same as request A and has been issued at the time as request A. No document is returned, which ensures the same document is not being processed twice.

Alternatives / Workarounds

parse-github-assistant[bot] commented 2 years ago

Thanks for opening this issue!

iMac7 commented 5 months ago

Add a method set or update to Parse.Query that if set executes Parse.Query.first as a findAndModify in MongoDB.

I don't get this part, would the new query look like, Parse.Query.set( ) or Parse.Query.first(val, opts) ?

mtrezza commented 5 months ago
const q = new Parse.Query();
q.equals("status", "upForGrabs");
q.set("status", "grabbed");
const obj = await q.first();

In a collection with N documents that match the query this query returns 1 matching document and at the same time sets status to grabbed, all in a single DB request using MongoDB's findAndModify operator.

We should look into the MongoDB docs to see whether set, update or modify make most sense semantically, to not cause confusion with other operators.

iMac7 commented 4 months ago

I see that a findOneAndModify is available in parse server already. Should this be solved in the sdk by conditionally changing the operation that Parse.Query.first() executes? Is there something to be done on the server?

mtrezza commented 4 months ago

Not sure if Parse Server supports findOneAndModify yet, even though it may be used internally for some operations. This would need to be implemented in the REST API, and I guess as you said, the client SDK should use that endpoint conditionally if the query contains a set.