D
D
DefaultX2018-07-23 17:21:40
MongoDB
DefaultX, 2018-07-23 17:21:40

How to get the actual value from MongoDB when querying from multiple workers?

There is a function in the worker that makes a request to the database, and updates the value in it so that other workers do not select the same document in the database. But it turns out that the rest of the workers select the same document as the first worker. How can I make it so that each worker selects his own document that is not used by the previous one. An approximate picture of how everything is now being done is shown in the diagram. Please tell me the best way to do this.
8384b36d77695e985fde9898f642f727.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DefaultX, 2018-07-24
@DefaultX

await Model.findOneAndUpdate({ inUse: false }, { inUse: true });

Problem solved.

E
Evgeny Kumanin, 2018-07-24
@jackkum

Move work with monga into a separate service with api and control the competition in this service.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question