Answer the question
In order to leave comments, you need to log in
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:
{
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,
},
{
group_id: 2,
group_label: 'Группа 2',
},
{
group_id: 4,
group_label: 'Группа 4',
},
{
group_id: 6,
group_label: 'Группа 6',
},
Lesson.find().select({ group_id: 1, group_label: 1 })
There are other fields in the document, so I use select. Answer the question
In order to leave comments, you need to log in
Model.findOne({group_id:value,group_label:value})
If I understood correctly, you need this method
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 questionAsk a Question
731 491 924 answers to any question