Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
JS: ordinate access to mongoDB

i have a little problem, i thought how to resolve it but i don't know if it's the right way.

in my project I have several functions, all of which must use a sequential unique id every time a function is executed

for example:

person 1 runs function X using id 1
person 2 runs function Y using id 2
person 3 runs function Z using id 3
person 4 runs function Y using id 4
person 1 runs function Z using id 5
....

how to do that?

I thought to save that id in a table inside mongoDB and every time it will used inside a function, the function will increment it, but i don't think it is the right way, it is?

then, if it is the right way, how to co-ordinate the access to the DB? like if 2 people runs 2 functions in the same time, the will take the same ID from DB and this absolutely must not happen, every time a function is called a different id must be used, how can I do?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use findOneAndUpdate

Example:

db.collection.findOneAndUpdate(
   { "name" : "function_trigger_count" },
   { $inc: { "count: 1 } }
)

FindOneAndUpdate ensure the atomicity of the query

OR

If you are using MongoDB version 5.x you can use Transaction feature to ensure the atomicity

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!