Travis
Travis

Reputation: 705

In Mongo is it possible to find documents with the same values for multiple fields?

I've been working with aggregates and can get it to work with one field but I cannot get i to work witht he use case in the title.

I have a collection of DVDs. I need to run a query that tries to identify duplicate DVDs based on three fields :

Heres an example document :

DVD
name : "Fargo",
director : "Cohen Brothers",
genre : "crime"

I want to use an aggregate that groups and returns documents whose three fields match but have been unable to do so. Is it even possible?

An example output based on similar functions, if there were 3 Fargos as above would be :

[['_id':'Fargo', 'size':3],['_id':'12 Angry Men', 'size':1] ]

Upvotes: 0

Views: 2750

Answers (1)

Travis
Travis

Reputation: 705

I found the answer straight after asking the question :/

This has been already answered on S/O here :

Mongodb Aggregation Framework | Group over multiple values?

You can group by a Map as follows :

$group : { _id : { name:'$name', director:'$director', genre:'$genre'} }

Upvotes: 2

Related Questions