Justin A
Justin A

Reputation: 4731

synchronous queries to MongoDB

is there a way to make synchronous queries to MongoDB?

I'd like to run some code only after I've retrieved all my data from the DB.

Here is a sample snipped.

Code Snippet A

const brandExists = Brands.find({name: trxn.name}).count();

Code Snippet B

if(brandExists == 0){
    Brands.insert({
                     name:trxn.name,
                     logo:"default.png", 
                 });
    Trxs.insert({
                   userId,
                   merchant_name,
                   amt,
               });
 }

I'd like Code snippet B to run only after Code Snippet A has completed its data retrieval from the DB. How would one go about doing that?

Upvotes: 2

Views: 1372

Answers (3)

Peppe L-G
Peppe L-G

Reputation: 8345

I don't think using an if statement like the one you have makes sense: the queries are sent after each other; it is possible someone else creates a brand with the same name as the one you are working with between your queries to the database.

MongoDB has something called unique indexes you can use to enforce values being unique. You should be able to use name as a unique index. Then when you insert a new document into the collection, it will fail if there already exists a document with that name.

https://docs.mongodb.com/manual/core/index-unique/

Upvotes: 1

Parth Raval
Parth Raval

Reputation: 4423

You can use simple async function async function always returns a promise.

const brandExists;
async function brandExist() {
  brandExists = Brands.find({
    name: trxn.name
  }).count();
}

brandExist().then(
  // Your Code comes here
  if (brandExists == 0) {
  Brands.insert({
    name: trxn.name,
    logo: "default.png",
  })
  Trxs.insert({
    userId,
    merchant_name,
    amt,
  });
});

Upvotes: 1

bordalix
bordalix

Reputation: 432

In Meteor, MongoDB queries are synchronous, so it already delivers what you need. No need to make any changes, snippet B code will only run after snippet A code.

When we call a function asynchronous we mean that when that function is called it is non-blocking, which means our program will call the function and keep going, or, not wait for the response we need.

If our function is synchronous, it means that our program will call that function and wait until it's received a response from that function to continue with the rest of the program.

Meteor is based in Node, which is asynchronous by nature, but coding with only asynchronous functions can origin what developers call "callback hell".

On the server side, Meteor decided to go with Fibers, which allows functions to wait for the result, resulting in synchronous-style code.

There's no Fibers in the client side, so every time your client calls a server method, that call will be asynchronous (you'll have to worry about callbacks).

Your code is server-side code, and thanks to Fibers you can be assure that snippet B code will only run after snippet A code.

Upvotes: 0

Related Questions