Need help with getting a Group by to work with data I get from a Distinct statement

Good afternoon,

I am currently working to write an aggregation query to pull data.  We have a collection that has a subfield that can group multiple records together.  An example would be we have a list of users that all have a unique "_id" field.  All those users can be grouped into an organization and each organization has a unique id for it as well '_organization.id'.

I am working to write an aggregation query that takes all the distinct ‘_organization.id’ records that are missing a specific field we will say ‘email’. and seeing which ‘_organization_id’ records have more then one user linked to that ‘_organization.id’

I am having issues writing this up utilizing the $group statement.

Any help would be greatly appreciated!

Hey Josh, it would be beneficial to see sample documents and the expected output that you are looking for. With that we can better help write a query to do what you want. Without it, we’re just making assumptions which is generally not a good thing to do. :wink:

Thank you for the advice. So what I am looking for is a list of all ‘_organization.id’ that have more then one user linked to it.

Example of data:
{
“_id” : “4ba107b0-9703-11e7-93f0-3780a90495c6”,
“provider” : “local”,
“hashedPassword” : “”,
“salt” : “xxxxxxxxxxxxxxxxxxxxxxxx”,
“_organization” : {
“id” : “364b5970-783f-11e7-9303-378fa672143b”,
“type” : “Organization”
},
“phone” : “555-555-5555”,
“email” : “test@test.com”,
“name” : “Test Record”,
“verification_code” : “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,
“type” : “User”,
“created” : ISODate(“2017-09-11T15:10:06.763Z”),
“role” : “admin”,
“crm_id” : null,
“is_verified” : false,
“is_active” : true,
“__v” : 0
}

Please read Formatting code and log snippets in posts and update your sample documents.