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

While updating how to add record if not found? #216

Closed
kingsanu opened this issue Dec 24, 2023 · 1 comment
Closed

While updating how to add record if not found? #216

kingsanu opened this issue Dec 24, 2023 · 1 comment
Labels
mongo query help help needed with mongodb querying (non-library related)

Comments

@kingsanu
Copy link

kingsanu commented Dec 24, 2023

await DB.Update<Author>()
        .Match(a => a.Surname == "Stark")
        .Modify(a => a.Name, "Brandon")
        .Modify(a => a.Surname, "The Broken")
        .ExecuteAsync();

i using Update method, but that entity is not found in database, so how can i add that entity if update not able to find record?

@dj-nitehawk
Copy link
Owner

https://www.mongodb.com/docs/v7.2/reference/method/db.collection.update/#std-label-update-upsert

await DB.Update<Author>()
        .Match(a => a.Surname == "Stark")
        .Modify(a => a.Name, "Brandon")
        .Modify(a => a.Surname, "The Broken")
        .Option(o => o.IsUpsert = true) //specify update options - upsert
        .ExecuteAsync();

@dj-nitehawk dj-nitehawk added the mongo query help help needed with mongodb querying (non-library related) label Dec 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mongo query help help needed with mongodb querying (non-library related)
Projects
None yet
Development

No branches or pull requests

2 participants