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

$elemMatch matches too many elements

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Works as Designed
    • Affects Version/s: 2.2.2
    • Fix Version/s: None
    • Component/s: Write Ops
    • Labels:
      None
    • Environment:
      Mac OS X 10.7.5.
    • Operating System:
      ALL
    • Steps To Reproduce:
      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.

      Description

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

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: