Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

want to group mongoDB documents by user. but after grouping, I don't want the user's mail field to appear again in the addresses. how?

My sample data:

[
    {
        'user': 'aa@hotmail.com',
        'AddressLine': 'jntgt',
        'vat': 'gfgf',
        'Companyname': 'gfgfg',
        'countryName': 'mexico',
        'AddressTitle': 'ev',
    },
    {
        'user': 'aa@hotmail.com',
        'AddressLine': 'gthh',
        'vat': 'gfgf',
        'Companyname': 'gfgfg',
        'countryName': 'usa',
        'AddressTitle': 'ev',
    },
    {
        'user': 'bb@hotmail.com',
        'AddressLine': 'gdgg',
        'vat': 'gfgf',
        'Companyname': 'ljjjhg',
        'countryName': 'Angola',
        'AddressTitle': 'ev',
    },
];

I want the result to look like this:

[
    {
        'user': 'aa@hotmail.com',
        'addresses': [
            {
                'AddressLine': 'jntgt',
                'vat': 'gfgf',
                'Companyname': 'gfgfg',
                'countryName': 'mexico',
                'AddressTitle': 'ev',
            },
            {
                'AddressLine': 'gthh',
                'vat': 'gfgf',
                'Companyname': 'gfgfg',
                'countryName': 'usa',
                'AddressTitle': 'ev',
            },
        ],
    },
    {
        'user': 'bb@hotmail.com',
        'addresses': [
            {
                'AddressLine': 'gdgg',
                'vat': 'gfgf',
                'Companyname': 'ljjjhg',
                'countryName': 'Angola',
                'AddressTitle': 'ev',
            },
        ],
    },
]

How should mongodb aggregation code be? I tried this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    {
        $group: {
            _id: '$user',
            addresses: { $push: '$$ROOT' },
        },
    },
    {
        $project: {
            user: '$_id',
            addresses: 1,
        },
    },
];

ok. but The user I grouped in root appears again. how can i remove user when i group.

>Solution :

You can do this in multiple ways, the easiest way IMO is to just add another $project stage at the end that excludes that field, like so:

db.collection.aggregate([
  {
    $group: {
      _id: "$user",
      addresses: {
        $push: "$$ROOT"
      }
    },
    
  },
  {
    $project: {
      user: "$_id",
      addresses: 1,
      
    }
  },
  {
    $project: {
      _id: 0,
      "addresses.user": 0
    }
  }
])

Mongo Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading