I
I
Ivan Ivanovich2021-06-16 09:55:52
Node.js
Ivan Ivanovich, 2021-06-16 09:55:52

Is there a way to optimize this approach?

Good day.

I have a user collection and related collections like emailConfirmation which refers to user.

I mean emailConfirmationModel:

userId: {
    type: Schema.Types.ObjectId,
    ref: 'User',
  },


When getting user data, I need to somehow get information about both the user and the emailConfirmation collection.

At the moment, these are 2 different queries to the database.

const user = await User.findOne...
const userConfirmationInfo = await emailConfirmation.findOne...


Next I tie them together
const data = { ...user,  ...userConfirmationInfo };


But I am almost certain that this is a crutch approach.

Can you please tell me how to optimize the solution of this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WapSter, 2021-06-16
@IwanQ

https://mongoosejs.com/docs/populate.html
Works like your code under the hood, i.e. sends multiple queries to the database

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question