Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement findOneAndReplace command #311

Closed
maheshrajamani opened this issue Mar 29, 2023 · 2 comments · Fixed by #324 or #345
Closed

Implement findOneAndReplace command #311

maheshrajamani opened this issue Mar 29, 2023 · 2 comments · Fixed by #324 or #345
Assignees

Comments

@maheshrajamani
Copy link
Contributor

maheshrajamani commented Mar 29, 2023

Replaces a single document based on the specified filter and sort. Returns a document in response.

{ "findOneAndReplace" : { "filter" : {filter clause}, "sort" : {sort clause}, "replacement" : {document to replace}, "options" : {"returnDocument" : "before/after"} } }

If upsert is true, insert the replacement document as new record.
if returnDocument is before return existing document, else return replaced document.

Validation: If the replacement document "_id" field is different from document read from the DB, throw error.

@maheshrajamani maheshrajamani self-assigned this Mar 29, 2023
@vkarpov15
Copy link
Collaborator

Also worth noting: you can omit _id in the replacement. So if _id is in replacement, it should be exactly equal to the _id in the db. But if omitted, findOneAndReplace() will use the existing document's _id.

@maheshrajamani
Copy link
Contributor Author

@johnsmartco My bad to have the upsert flag in the sample here. It's not supported for findOneAndReplace command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants