Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9247719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:48:09+00:00 2026-06-18T09:48:09+00:00

Documents: { group : G1, cat : Cat1, desc: Some description 1} { group

  • 0

Documents:

{ "group" : "G1", "cat" : "Cat1", "desc": "Some description 1"}
{ "group" : "G1", "cat" : "Cat2", "desc": "Some description 2"}
{ "group" : "G1", "cat" : "Cat1", "desc": "Some description 3"}
{ "group" : "G1", "cat" : "Cat3", "desc": "Some description 4"}
{ "group" : "G1", "cat" : "Cat2", "desc": "Some description 4"}

Can someone help me, using Mongoose, how to find the records that have unique group and cat?

From the Mongoose API for distinct, I understand the that I can use just one field. But can the Model.distinct be used to find documents based on two fields?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:48:10+00:00Added an answer on June 18, 2026 at 9:48 am

    I can’t give you a Mongoose specific example, and your question is a bit vague. The aggregation equivalent of “But can the Model.distinct be used to find documents based on two fields?” is:

    db.test.aggregate( { $group: { _id: { group: "$group", cat: "$cat" } } } );
    

    Which returns:

    {
        "result" : [
            {
                "_id" : {
                    "group" : "G1",
                    "cat" : "Cat3"
                }
            },
            {
                "_id" : {
                    "group" : "G1",
                    "cat" : "Cat2"
                }
            },
            {
                "_id" : {
                    "group" : "G1",
                    "cat" : "Cat1"
                }
            }
        ],
        "ok" : 1
    }
    

    If you want to find group/cat combinations that only occur once, then you would use:

    db.test.aggregate(
        { $group: {
            _id: { group: "$group", cat: "$cat" }, 
            c: { $sum: 1 }, 
            doc_ids: { $addToSet: "$_id" }
        } },
        { $match : { c: 1 } }
    );
    

    Which returns:

    {
        "result" : [
            {
                "_id" : {
                    "group" : "G1",
                    "cat" : "Cat3"
                },
                "c" : 1,
                "doc_ids" : [
                    ObjectId("5112699b472ac038675618f1")
                ]
            }
        ],
        "ok" : 1
    }
    

    From http://mongoosejs.com/docs/api.html#model_Model.aggregate I learn that you can use the aggregation framework in Mongoose like:

    YourModel.aggregate(
        { $group: { _id: { group: "$group", cat: "$cat" } } },
        function(err, result) {
            console.log(result)
        }
    ) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to group and sort by date_published some documents stored on mongodb using
I print documents (batch print) using Word.Document.PrintOut method. How can I print a document
Making my way through the GAE documents. I have a question I can't find
Is it possible that i can post and retrieve a group's documents with the
I created a docx document using OpenXML SDK. It contains group content controls to
I am validating XML documents against a schema. Some more complex documents/schemas always fail
I'm inserting documents using node mongodb native driver into a mongodb (sic!). My objects
I have a Group entity which has many Item entities. An Item entity can
In a MySQL database I have documents of different type: some have text content,
I'm currently building a class in PHP that generates PDF documents using the WKHTMLTOPDF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.