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

Find duplicate name in MongoDB

I’m having a problem in getting the duplicate name in my mongodb to delete duplicates.

{
    "users": [
      {
        "_id": {
          "$oid": "61441890a6566a001623b8ed"
        },
        "name": "Jollibee",
      },
      {
        "_id": {
          "$oid": "61441890a6566a001623b8ed"
        },
        "name": "Jollibee",
      },
      {
        "_id": {
          "$oid": "61441890a6566a001623b8ed"
        },
        "name": "MCDO",
      },
      {
        "_id": {
          "$oid": "61441890a6566a001623b8ed"
        },
        "name": "Burger King",
      },
    ]
  }

I want to show in my output only the duplicate names. which is Jollibee.

tried this approach but it only returns me the count of all the users not the duplicated ones. I want to show 2 Jollibee only.

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

db.collection.aggregate([
  {
    "$unwind": "$users"
  },
  {
    "$group": {
      "_id": "$_id",
      "count": {
        "$sum": 1
      }
    }
  },
  {
    "$match": {
      "_id": {
        "$ne": null
      },
      "count": {
        "$gt": 1
      }
    }
  }
])

>Solution :

since the $unwind step gives you same _id for all documents grouping by _id is not correct. Instead try grouping by users.name

db.collection.aggregate([
  {
    "$unwind": "$users"
  },
  {
    "$group": {
      "_id": "$users.name",
      "count": {
        "$sum": 1
      }
    }
  },
  {
    "$match": {
      "_id": {
        "$ne": null
      },
      "count": {
        "$gt": 1
      }
    }
  }
])

demo

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