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

Incosistent behavior with $or and $elemMatch

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: 2.4.8, 2.6.3
    • Fix Version/s: None
    • Component/s: Querying, Write Ops
    • Labels:
      None
    • Environment:
      Mac OS X 10.7.5.
    • Operating System:
      ALL
    • Steps To Reproduce:
      Hide

      If you do queries:

      > db.test.insert({nested: [{a: 1, slug: 'aaa'}, {a: 1, slug: 'aaa'}, {a: 2, slug: 'aaa'}]})
      > db.test.find()
      { "_id" : ObjectId("52763c5cb6786ce4ff406cbf"), "nested" : [ { "a" : 1, "slug" : "aaa" }, { "a" : 1, "slug" : "aaa" }, { "a" : 2, "slug" : "aaa" } ] }
      > db.test.update({'nested.a': 1, $or: [{'nested': {$elemMatch: {a: 1, slug: {$exists: true}}}}]}, {$unset: {'nested.$.slug': ''}})
      > db.test.find()
      { "_id" : ObjectId("52763c5cb6786ce4ff406cbf"), "nested" : [ { "a" : 1 }, { "a" : 1, "slug" : "aaa" }, { "a" : 2, "slug" : "aaa" } ] }
      > db.test.update({'nested.a': 1, $or: [{'nested': {$elemMatch: {a: 1, slug: {$exists: true}}}}]}, {$unset: {'nested.$.slug': ''}})
      > db.test.find()
      { "_id" : ObjectId("52763c5cb6786ce4ff406cbf"), "nested" : [ { "a" : 1 }, { "a" : 1, "slug" : "aaa" }, { "a" : 2, "slug" : "aaa" } ] }

      I would expect that the second update will remove slug from second a == 1 array element. But it does not. On the other hand, if I run:

      db.test.update({'nested.a': 1, 'nested': {$elemMatch: {a: 1, slug: {$exists: true}}}}, {$unset: {'nested.$.slug': ''}})

      Second slug is correctly removed.

      Show
      If you do queries: > db.test.insert({nested: [{a: 1, slug: 'aaa'}, {a: 1, slug: 'aaa'}, {a: 2, slug: 'aaa'}]}) > db.test.find() { "_id" : ObjectId("52763c5cb6786ce4ff406cbf"), "nested" : [ { "a" : 1, "slug" : "aaa" }, { "a" : 1, "slug" : "aaa" }, { "a" : 2, "slug" : "aaa" } ] } > db.test.update({'nested.a': 1, $or: [{'nested': {$elemMatch: {a: 1, slug: {$exists: true}}}}]}, {$unset: {'nested.$.slug': ''}}) > db.test.find() { "_id" : ObjectId("52763c5cb6786ce4ff406cbf"), "nested" : [ { "a" : 1 }, { "a" : 1, "slug" : "aaa" }, { "a" : 2, "slug" : "aaa" } ] } > db.test.update({'nested.a': 1, $or: [{'nested': {$elemMatch: {a: 1, slug: {$exists: true}}}}]}, {$unset: {'nested.$.slug': ''}}) > db.test.find() { "_id" : ObjectId("52763c5cb6786ce4ff406cbf"), "nested" : [ { "a" : 1 }, { "a" : 1, "slug" : "aaa" }, { "a" : 2, "slug" : "aaa" } ] } I would expect that the second update will remove slug from second a == 1 array element. But it does not. On the other hand, if I run: db.test.update({'nested.a': 1, 'nested': {$elemMatch: {a: 1, slug: {$exists: true}}}}, {$unset: {'nested.$.slug': ''}}) Second slug is correctly removed.

      Description

      There seems to be inconsistency when using $or in combination with $elemMatch. See steps to reproduce.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              david.storch David Storch
              Reporter:
              mitar Mitar
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: