0

I have a collection with this fixed structure:

enter image description here

By fixed structure I mean I just push Objects to the level arrays which are starter - intermediate and advanced as you see.

Now suppose you want to fetch intermediate array only, I can do it like this and it works fine:

const level = 'intermediate';
const motherModel = await db.Mother.findOne({});
const motherLevel = motherModel.cards[level]; // here we can get the specified level

This approach I need to retrieve all the data from database and save it to memory which may not be the best practice nor the fastest way because we only want to get the intermediate level right?

Is there any better ways to do this? or I'm totally wrong and I can be fine with this approach?

MrMythical
  • 8,908
  • 2
  • 17
  • 45
Sara Ree
  • 3,417
  • 12
  • 48
  • Does this answer your question: https://stackoverflow.com/a/25589150/14389830 – NeNaD Jul 16 '21 at 17:28
  • Does this answer your question? [How to select a single field for all documents in a MongoDB collection?](https://stackoverflow.com/questions/25589113/how-to-select-a-single-field-for-all-documents-in-a-mongodb-collection) – NeNaD Jul 16 '21 at 17:29
  • Please guys note that I have no conditions to retrieve data. in the above links it's just like other samples on the net.. – Sara Ree Jul 16 '21 at 18:33
  • @SaraRee just project `.findOne({}, { ["cards."+level]: 1 })` – turivishal Jul 17 '21 at 03:42

0 Answers0