0

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.

USer22999299
  • 5,284
  • 9
  • 46
  • 78

1 Answers1

1

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 }
        }
    }
]);
Constantin Guay
  • 1,604
  • 2
  • 15
  • 26