title: Search MongoDB Array of Objects where property has same value for more than 1 array element
body: <p>I have a Mongo Collection of Products with a Categories field that is an array of objects.</p>
  
  <pre><code>{
      "_id" : ObjectId("XXX"),
      "title" : "Cool Product",
      "details" : "Some Details",
      "categories" : [ 
          {
              "name" : "Cat1",
              "isPrimary" : true
          }, 
          {
              "isPrimary" : true,
              "name" : "Cat2"
          }, 
          {
              "name" : "Cat3"
          }
      ]
  }
  </code></pre>
  
  <p>Because a product can have more than one category, I wanted to force a primary category relationship (one-to-one). However, in data migration some documents have the isPrimary property true for multiple categories in the document. I need to find the products where isPrimary is true for more than one array element in the categories array. This is what I have so far:</p>
  
  <pre><code>db.products.find({ "categories" : { "$elemMatch" : { "isPrimary" : { "$exists" : false}}}})
  </code></pre>
  
  <p>But this only gives me the results where isPrimary doesn't exist for one of the array elements. I can't figure out how to query isPrimary having the same value on multiple array elements. Also, this will be a Spring query as well:</p>
  
  <pre><code>Query query = new Query();
  query.addCriteria(new Criteria().orOperator(
              Criteria.where("categories").elemMatch(Criteria.where("isPrimary").exists(false)),
              Criteria.where("categories").size(0),
              Criteria.where("categories")
              ));
  query.with(new Sort(Sort.Direction.ASC, "title"));
  return operations.find(query, Product.class);
  </code></pre>