title: MongoDB unique compound index on nested array
body: <p>Suppose I have following document:</p>
  
  <pre><code>{
          "_id" : ObjectId("5a4142699f6e958126baa794"),
          "name" : "doc7",
          "nestedObjects" : [
                  {
                          "val" : 15
                  },
                  {
                          "val" : 16
                  },
                  {
                          "val" : 15
                  }
          ]
  }
  </code></pre>
  
  <p>What would be the spell in MongoDB shell to enforce uniqueness of "val" values in nestedObjects array, within a particular doc, so  name/val combinations should be unique?</p>
  
  <p>so far I tried db.test.docs.ensureIndex({"name":1, "val": 1}, {unique: true})
  and db.test.docs.createIndex({"name":1, "val": 1}, {unique: true}) but nothing seems to work. When I try to insert:</p>
  
  <pre><code>db.test.docs.find().forEach(function(doc){
      db.test.docs.update({_id: doc._id}, {$push:{"nestedObjects": {val: 15}}})}); 
  </code></pre>
  
  <p>Mongo DB doesn't mind that...</p>