Skip to content

Patch items in MongoDB with upsert option does not return patched items as result #3563

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

Open
claustres opened this issue Feb 6, 2025 · 1 comment

Comments

@claustres
Copy link
Contributor

claustres commented Feb 6, 2025

The MongoDB adapter allow to pass specific MongoDB options in params.mongodb. When updating items MongoDB supports the upsert option in order to create them if not already exist. However, in this specific case, when patching items not yet existing with the upsert option the adapter will not return any result as it seems to retrieve the list of patched object first, which is empty.

Not sure why the list of patched items is retrieved first instead of once patched, maybe a specific check should be done on the upsert option to use the most appropriate way to retrieve the patched objects.

@DaddyWarbucks
Copy link
Member

I see what you mean. I can answer why the list is pre-fetched and the re-fetched. This is to prevent wrong queries when the user both queries and mutates a property in the same call. This is a common pattern in the database adapters for multi patch. For example,

const user = await service.patch(null, { name: 'Johnny Cash' }, { query: { name: 'June Cash' } ));

Because we queried by June Cash and also updated the name to Johnny Cash, we cannot then re-query by June Cash because it is no longer valid.

I do see that the updateMany function does return upsertedId and upsertedCount . So we could probably get those and add them to the idList to re-fetch.

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

No branches or pull requests

2 participants