0

I want to select frequence >1 records.

Take the following data for example,

It should give me the result like, [{id: 146}]

because only the id 146 occurs more than one.

Ho wcould I do this in mongoDB, thanks

{
    id: 146,
    name: Mary
}

{
    id: 148,
    name: Jack
}

{
    id: 146,
    name: Mary
}
user3675188
  • 7,271
  • 11
  • 40
  • 76
  • Do you want to get _array of embedded documents_ (having only id field) for which name is reating ? – Dev Apr 06 '15 at 08:23
  • possible duplicate of [Find all duplicate documents in a MongoDB collection by a key field](http://stackoverflow.com/questions/9491920/find-all-duplicate-documents-in-a-mongodb-collection-by-a-key-field) – Neo-coder Apr 06 '15 at 11:17

1 Answers1

1

You should explain correctly what you want. Also you should provide sample data correctly.

MongoId is a unique field that can not be repeated, but you can find multiple documents on the basis of any other key. According to your sample data, you can write query like following -

db.collection.aggregate([{$group:
{"_id":{"id":"$id","name":"$name"},
uniqueIds: { $addToSet: "$_id" }, //u can comment out this line if not required
count: { $sum: 1 }
}},
{ $match: {
count: { $gt: 1 }
}}])

This will return all documents with count(of id) greater than one.

Vishwas
  • 6,967
  • 5
  • 42
  • 69
  • @ user3675188 updated my answer. Is it you looking for?? You can remove uniqueIds field if you do not require. Also you can group data on other keys also. – Vishwas Apr 06 '15 at 10:25