What is the best way to ensure that the same object is not selected multiple times when executing concurrent queries

Currently, I am developing a delivery service that processes 8 orders simultaneously, each requiring a unique consignment number to be stored in the database. However, due to the concurrent nature of the operations, the system is assigning the same object/consignment number to all orders.

const query = new Parse.Query("Consign")
        query.doesNotExist("order")
        query.equalTo("staging", staging)
        query.exists("number")
        query.limit(1)
        await query.find().then(function (result) {
            if (result.length > 0) {
                order.set("consign_no", result[0].get("number"))
                result[0].set("order", order)
            } else {
                //failed
            }
        }).catch((err)=>{
            console.log(err);
        })

Is there a way to modify this query to ensure unique objects are picked up even when executed concurrently?

Answer №1

After reviewing a response provided by another individual:

  • Establish a counter repository
  • Add a single document to it containing {value:1}
  • Whenever a value is required, employ find-and-modify to $inc this document and provide the updated value

This method should be effective (pending confirmation on Whether updating the same document in concurrent findAndModify operations in Mongo is feasible?) in ensuring distinctive values. Note that if a value retrieved at a later time is initially inserted, the sequence of values in your documents may not be consecutive.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Encountering an Error with Route.get() when attempting to utilize an imported

I have a function that I exported in index.js and I want to use it in test.js. However, when I try to run node test, I encounter the following error message: Error: Route.get() requires a callback function but got a [object Undefined] What am I doing wro ...

Iframe navigation tracking technology

Let's say I have an iframe element in my HTML document. <html> <body> This is my webpage <button> Button </button> <iframe src="www.example.com"></iframe> </body> </html> If a user clicks on links wi ...

retrieving the site's favicon icon

Currently, I am attempting to extract favicons from website URLs using the HtmlAgilityPack library. While I have been successful in retrieving some favicons, there are still some that remain elusive. I suspect that the inconsistency lies in the implementat ...

Is there a way to limit the width of the input box once it reaches a specific pixel measurement?

One issue I'm facing involves the following code snippet: <html> <head> <script type="text/javascript"> function Expand(obj){ if (!obj.savesize) obj.savesize=obj.size; obj.size=Math.max(obj.savesize,obj.v ...

The component name 'Hidden' is not valid for use in JSX

Currently, I'm immersed in a personal project focused on creating a responsive website utilizing Material-UI. In this endeavor, I've leveraged React and kickstarted the project with create-react-app. To enhance the design, I incorporated code fro ...

Establishing a TCP connection to a server using Javascript

Currently, I have developed a server daemon that generates various data, such as messages. However, my main focus is on client monitoring. For instance, I have a webpage and I aim to maintain a constant TCP connection to the server in order to display all ...

Highchart displays text centrally on legend hover

I am struggling with the code provided here. My goal is to make text appear in the center of the donut chart when hovering over the legend. Similar to how it works when hovering over a single piece of the donut chart. var chart = new Highcharts.Chart ...

Refreshing the information in the database table

Upon receiving data from the server using ajax, I populate this table: $.each(data, function(i, item) { $('#MyTable tbody').append("<tr>" +"<td>" +data[i].A+ "</td><td>" +data[i].B ...

Having trouble with Laravel routes and jQuery $.post()? Keep getting a frustrating 404 Not Found error?

A JavaScript file with jQuery that is responsible for sending a POST request $.post('log_in', { email: email, password: password }, function(response) { $('#log_in_result').html(response); console.log(response); }); In the Lar ...

Styling HTML elements with CSS to create a full width underline effect

Is there a way to make the underlines/borders full width for each line in a paragraph without adding line breaks? I'm seeking suggestions on how to achieve this. Two potential solutions I've considered are using the tag or creating an image, ...

How do I utilize Protractor to target a specific div element with a distinctive class and verify its visibility on the page?

Below is the view.html for a unique class: <div class="appExperience small-tile"> </div> This is the code snippet that I attempted to use: var displayedTile = element(by.css('.appExperience small-tile')); expect(displayedTile.i ...

Aurelia validator fails to refresh user interface

Despite the aurelia-validator plugin working correctly for form submission and validation, with all properties updating properly, the UI does not reflect any changes. There is no red outline around incorrect properties or error messages displayed. I have r ...

Error: Unable to establish connection with local host (::1) on port 50106

I am currently in the process of developing a Discord bot using Node.js and erela.js. However, I encountered an error when attempting to retrieve the server that handles erela: A node error occurred: connect ECONNREFUSED ::1:50106 2020-05-01T21:23:19.367 ...

Updating the code for Express to store JSON data in a database field

Struggling with setting up my first basic CRUD functionality in Express JS and I can't seem to track down this frustrating bug. Every time I attempt to update a field, instead of displaying the new data, the JSON from that field is shown on the view. ...

Incorporating interactive maps into an AngularJS web application

I've been attempting to integrate Google Maps into my AngularJS application, using the script tag below: <script src="https://maps.googleapis.com/maps/api/js?key=[MySecretKeyHere]&callback=initMap" async defer></script> I found this ...

Incorporating the non-typescript npm package "pondjs" into Meteor applications using typescript files

Implementing the Pondjs library into my project seemed straightforward at first: meteor npm install --save pondjs However, I'm encountering difficulties when trying to integrate it with my Typescript files. The documentation suggests: In order ...

Securing Communication with HTTPS in Express JS

After purchasing an AlphaSSL from a hosting provider, I received the following files: domain.csr.crt domain.interCert.crt domain.PKCS7Cert.crt domain.rootCert.crt domain.X509Cert.crt However, in order to enable HTTPS on Node.JS using Express, I am aware ...

Steps for generating a div, link, and image that links to another image using Javascript

Hey there, I have a cool picture to share with you! <img src="cards.png" id="img"> <!--CARD PICTURE--> Check out what I want to do with it: <div class="container_img"> <img src="cards.png" id="img"> <!--CARD PICTURE--> ...

"Converting GMT date time to a Unix TimeStamp in GMT with JavaScript: A Step-by-Step

There are a multitude of methods available for converting date time into Unix timestamp. The issue arises when trying to convert the date time of GMT into Unix timestamp as it displays the value of the timestamp based on my local timezone (Asia/Kolkata). ...

Employing a "cross-domain" approach to serve as a login gateway for a different domain

I need to configure the domain: aaaa.com so that it can host a login form for the website located at domain: cccc.com. It's important to note that I have complete control over the server at cccc.com and have successfully set up CORS on that server. A ...