A
A
Andrey2019-10-31 18:34:52
JavaScript
Andrey, 2019-10-31 18:34:52

How to get top records in Mongodb?

Hello, do not judge strictly, but how to get the top 50 records by the number of mentions through mongoose, for example, there is a type base

[{
name: "n1",
text: "n1"
},
{
name: "n1",
text: "n1"
},
{
name: "n2",
text: "n2"
},]

in the end i need an array like
[{
name: "n1",
text: "n1"
count: 2
},
{
name: "n2",
text: "n2",
count: 1
}]

in sql it would be like
SELECT name, text, COUNT(name) AS `count` 
FROM table
GROUP BY name, text

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2019-11-14
@grinat

https://mongoosejs.com/docs/api/aggregate.html#agg...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question