N
N
Nurasyl Aldan2021-08-23 16:36:59
MongoDB
Nurasyl Aldan, 2021-08-23 16:36:59

How to query like select ... for update in MongoDB?

How to query like select ... for update in MongoDB?
I am using the mongodb driver in NodeJS.

I want to use something like this: I do
n’t see examples in the documentation, it’s hard to understand.

let user = await db.users.findOne({user: 1}).toArray()
if(user.balance <= 100) {
user.balance = 100
}
user.save()

Whatever there are conflicts, dirty readings, etc.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Masson, 2021-08-23
@pumbasl

findOneAndUpdate()
You can also do the same as you indicated an example from the documentation.
In your example, you enter the user into a variable, then change the field in it, after which you save, what is not clear?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question