Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-11536

$elemMatch matches too many elements

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.2.2
    • Component/s: Write Ops
    • None
    • Environment:
      Mac OS X 10.7.5.
    • ALL
    • Hide

      If you do query:

       db.test.insert({nested: [{a: 1}, {a: 1}, {a: 2}]})
      > db.test.find()
      { "_id" : ObjectId("52762cca359e2dd78d27d30e"), "nested" : [ { "a" : 1 }, { "a" : 1 }, { "a" : 2 } ] }
      > db.test.update({'nested.a': 1, nested: {$elemMatch: {slug: {$ne: 'aaa'}}}}, {$set: {'nested.$.slug': 'aaa'}})
      > db.test.find()
      { "_id" : ObjectId("52762cca359e2dd78d27d30e"), "nested" : [ { "a" : 1, "slug" : "aaa" }, { "a" : 1 }, { "a" : 2 } ] }
      > db.test.update({'nested.a': 1, nested: {$elemMatch: {slug: {$ne: 'aaa'}}}}, {$set: {'nested.$.slug': 'aaa'}})
      > db.test.find()
      { "_id" : ObjectId("52762cca359e2dd78d27d30e"), "nested" : [ { "a" : 1, "slug" : "aaa" }, { "a" : 1, "slug" : "aaa" }, { "a" : 2 } ] }
      > db.test.update({'nested.a': 1, nested: {$elemMatch: {slug: {$ne: 'aaa'}}}}, {$set: {'nested.$.slug': 'aaa'}})
      > db.test.find()
      { "_id" : ObjectId("52762cca359e2dd78d27d30e"), "nested" : [ { "a" : 1, "slug" : "aaa" }, { "a" : 1, "slug" : "aaa" }, { "a" : 2, "slug" : "aaa" } ] }
      

      Array element with a == 2 should not be updated. At least this is not expected.

      Show
      If you do query: db.test.insert({nested: [{a: 1}, {a: 1}, {a: 2}]}) > db.test.find() { "_id" : ObjectId( "52762cca359e2dd78d27d30e" ), "nested" : [ { "a" : 1 }, { "a" : 1 }, { "a" : 2 } ] } > db.test.update({ 'nested.a' : 1, nested: {$elemMatch: {slug: {$ne: 'aaa' }}}}, {$set: { 'nested.$.slug' : 'aaa' }}) > db.test.find() { "_id" : ObjectId( "52762cca359e2dd78d27d30e" ), "nested" : [ { "a" : 1, "slug" : "aaa" }, { "a" : 1 }, { "a" : 2 } ] } > db.test.update({ 'nested.a' : 1, nested: {$elemMatch: {slug: {$ne: 'aaa' }}}}, {$set: { 'nested.$.slug' : 'aaa' }}) > db.test.find() { "_id" : ObjectId( "52762cca359e2dd78d27d30e" ), "nested" : [ { "a" : 1, "slug" : "aaa" }, { "a" : 1, "slug" : "aaa" }, { "a" : 2 } ] } > db.test.update({ 'nested.a' : 1, nested: {$elemMatch: {slug: {$ne: 'aaa' }}}}, {$set: { 'nested.$.slug' : 'aaa' }}) > db.test.find() { "_id" : ObjectId( "52762cca359e2dd78d27d30e" ), "nested" : [ { "a" : 1, "slug" : "aaa" }, { "a" : 1, "slug" : "aaa" }, { "a" : 2, "slug" : "aaa" } ] } Array element with a == 2 should not be updated. At least this is not expected.

      When using $elemMatch with normal field selectors they are not taken as union so more than expected array elements are matched.

            Assignee:
            ramon.fernandez@mongodb.com Ramon Fernandez Marina
            Reporter:
            mitar NOVALUE Mitar
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: