title: How do I $pull array element by field value (when elements are objects)
body: <p>I am using MongoDB and trying to remove array elements (themselves embedded documents) from documents in a DB matching a criteria. FOr this I am trying to use the $pull operator in the update command. But I am unable to make this work in some cases (See description below). What am I missing?</p>
  
  <p>Thanks in advance.</p>
  
  <p>-Sachin</p>
  
  <pre><code>&gt; use test
  switched to db test
  
  //First, insert a record with an array of addresses, with array elements being embedded objects with exactly 1 element (email)
  &gt; db.users.insert({
      name: 'smith',
      addresses:[{email:'a@b'},{email:'c@d'}]
      });... ... ...
  
  
  //Result of the insertion
  &gt; db.users.find()
  { "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "a@b" }, { "email" : "c@d" } ] }
  
  
  //From records with name= Smith, try to $pull any array elements with email a@b
  
  &gt; db.users.update({name:'smith'}, {$pull:{addresses:{email:'a@b'}}});&gt; 
  
  //After successful $pull
  
  &gt; db.users.find()
  { "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c@d" } ] }
  
  
  //Now insert  a record with an array of addresses, with array elements being embedded objects with exactly 2 elements (email, phone)
  
  &gt; db.users.insert({
      name: 'smith',
      addresses:[{email:'a@b', phone: '12345'},{email:'c@d',phone :'54321'}]
      });... ... ... 
  
  
  //Result of the insertion
  
  &gt; db.users.find()
  { "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c@d" } ] }
  { "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
          {
                  "email" : "a@b",
                  "phone" : "12345"
          },
          {
                  "email" : "c@d",
                  "phone" : "54321"
          }
  ] }
  
  //From records with name= Smith, again try to $pull any array elements with email a@b
  
  &gt; db.users.update({name:'smith'}, {$pull:{addresses:{email:'a@b'}}})
  
  
  // - Unsuccessful $pull (Why? How to fix this)
  &gt; db.users.find()
  { "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c@d" } ] }
  { "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
          {
                  "email" : "a@b",
                  "phone" : "12345"
          },
          {
                  "email" : "c@d",
                  "phone" : "54321"
          }
  ] }
  
  
  //Meanwhile, the single element pull still works as before -
  
  &gt; db.users.update({name:'smith'}, {$pull:{addresses:{email:'c@d'}}})
  
  
  &gt; db.users.find()
  { "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
  { "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
          {
                  "email" : "a@b",
                  "phone" : "12345"
          },
          {
                  "email" : "c@d",
                  "phone" : "54321"
          }
  ] }
  &gt; 
  </code></pre>
  
  <p>Thanks for the resposen, although that didn't work. Here is the transcript of the Mongo shell.   </p>
  
  <pre><code>&gt; db.users.find()
  { "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
  { "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
          {                               
                  "email" : "a@b",
                  "phone" : "12345"
          },
          {
                  "email" : "c@d",
                  "phone" : "54321"
          }
  ] }
  &gt; db.users.update({name:'smith'}, {$pull:{"addresses.email": 'a@b'}})
  Modifier spec implies existence of an encapsulating object with a name that already represents a non-object, or is referenced in another $set clause
  &gt; db.users.find()
  { "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
  { "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
          {
                  "email" : "a@b",
                  "phone" : "12345"
          },
          {
                  "email" : "c@d",
                  "phone" : "54321"
          }
  ] }
  &gt; 
  </code></pre>
  
  <p>...so basically the dot notation didnt work out.</p>