Search code examples
mongodbmongodb-querymongodb-shell

How to get the list of docs that exist more than 1 time in mongoDB with the same field?


I would like to create a query that will get all the docs with title that exist more than 1 time in mongoDB.

lets say that this is my doc:

{
  "itemID" : "AAN88998JJCA",
  "itemTitle" : "AAAA
}

{
  "itemID" : "AAN8BB98JJCA",
  "itemTitle" : "AAAA"
}

{
  "itemID" : "A5N84998JJ3A",
  "itemTitle" : "AACC"
}

{
  "itemID" : "A2N81998JJC1",
  "itemTitle" : "AACC"
}

{
  "itemID" : "A2N81998JJC1",
  "itemTitle" : "BBBB"
}

I would like to set a query that will produce a list of

 {
      "itemID" : "AAN88998JJCA",
      "itemTitle" : "AAAA
    }

    {
      "itemID" : "AAN8BB98JJCA",
      "itemTitle" : "AAAA"
    }

    {
      "itemID" : "A5N84998JJ3A",
      "itemTitle" : "AACC"
    }

    {
      "itemID" : "A2N81998JJC1",
      "itemTitle" : "AACC"
    }

Which mean a record with the same title that exist in the DB more than 1 time. I can do it in Java code but seem like it would be more reasonable to do it on the DB level.


Solution

  • Using aggregation framework, you can group itemTitle by the number of occurences:

    db.collection.aggregate([
        {
            $group: {
                _id: "$itemTitle",
                total: { $sum: 1 }
            }
        }
        ,{
            $match: {
                total: { $gt: 1 }
            }
        }
    ]);