4

How do you increment multiple fields in one query in mongoose?

For example, here is the working mongo query that I want to use with a mongoose model.

db.users.update({ userId:89 }, { $inc : { "subjectResults.attempts" : 1, "subjectResults.total_time" : 10, "subjectResults.total_score" : 100 } })

The above query is working in plain vanilla Mongo.

Edit: I have removed the confusing property name that lead to the comments below.

0

1 Answer 1

6

The APIs are nearly identical. Instead of db.users.update in straight mongodb, it's UserModel.update in mongoose. Pass in the exact same query and update options object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was hoping there might be a more elegant way to do it when dealing with model instances but that makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.