0

Here is a sample of the collection I have in MongoDB called list

    {
        "Id" : 1,
        "Name" : "Music",
        "PicId" : 1
    },
    {
        "Id" : 3,
        "Name" : "Movies",
        "PicId" : 2
    },
    {
        "Id" : 2,
        "Name" : "Games",
        "PicId" : 4
    },

I need to create a query that will show me which id has the most pics?

and I need to show the Id and counts of pics for each Id in my output.

I have tried to create the query but so far it does not give me the correct results.

Any suggestions on how I should create this MongoDB query?

4
  • 2
    What is your query? What does it return? What do you want it to return instead?
    – mustaccio
    Dec 9 '21 at 20:33
  • I need to create a query that will show me which id has the most pics? and I need to show the Id and counts of pics for each Id in my output. @mustaccio
    – abc
    Dec 9 '21 at 20:36
  • 1
    There's no need to repeat the vague description from your question. Don't tell us, show us (by adding the missing information to your question) your current query, the result you get, and the result you want (that would presumably be in the JSON format, just like the sample data you have included).
    – mustaccio
    Dec 9 '21 at 20:56
  • You are trying an aggregation to calculate the counts and the maximum value. You can use the $group aggregation stage and the operators $sum and $max.
    – prasad_
    Dec 10 '21 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.