D
D
Di Ma2020-12-30 01:05:02
MongoDB
Di Ma, 2020-12-30 01:05:02

How to make multiple queries at once in mongodb?

Hello.
The page displays several categories of products. For each category, you have to make a request and wait for a response in the callback function. Those. before filling the template with data, you need to make sure that all requests have received answers, and this is an extra, not beautiful code.
Can I somehow get an object at once that would have something like this?

{
    cat_1: {ответ на первый запрос},
    cat_2: {ответ на второй запрос}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-12-30
@dimentimor

somewhere in your async function

return {
 cat_1: await loadForCategory('cat_1'),
 cat_2:  await loadForCategory('cat_2'),
}

Requests will go in parallel to mongo if there are free connections in the pool.
loadForCategory - access mongo in them, return a promise from mongo, the mongo client can return them, callbacks are not needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question