M
M
Movsar-Khalakhoev2021-03-31 14:11:12
MongoDB
Movsar-Khalakhoev, 2021-03-31 14:11:12

How to make a proper query in mongoose?

Hello!

I have a collection of documents where there are keys group_id, group_label. These keys may be the same for different documents. I need to get a list of objects where there will be only one object with the same group_id and group_label, even if there are several documents where these keys are the same.

Example:
Collection:

spoiler
{
        group_id: 4,
        group_label: 'Группа 4',
      },
      {
        group_id: 2,
        group_label: 'Группа 2',
      },
      {
        group_id: 4,
        group_label: 'Группа 4',
      },
      {
        group_id: 6,
        group_label: 'Группа 6',
      },
      {
        group_id:  2,
        group_label: 'Группа 2,
      },


Return data:
spoiler
{
        group_id: 2,
        group_label: 'Группа 2',
      },
      {
        group_id: 4,
        group_label: 'Группа 4',
      },
      {
        group_id: 6,
        group_label: 'Группа 6',
      },


How can I make such a request? Now the request looks like this:
Lesson.find().select({ group_id: 1, group_label: 1 })
There are other fields in the document, so I use select.

Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem, 2021-03-31
@Kp18

Model.findOne({group_id:value,group_label:value})
If I understood correctly, you need this method

Y
yevgeniypak0420, 2021-03-31
@yevgeniypak0420

Hello. I am a beginner in this whole topic, I googled on English-language sites and found this solution:
Lesson.distinct("group_id").then()
Lesson.distinct("group_label").then()
True, in the end we will get not a full-fledged object, but arrays with unique data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question