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

The $exists operator is not working correctly

    • Type: Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.0.4
    • Component/s: Querying
    • Labels:
      None
    • ALL
    • Hide

      I'm trying to pull nested documents from a field of type array called "workers"  that is part of each document of a collection named "production_units", matching the condition that the nested document shouldn't contain 'worker_number' field.

      In fact I have a nested document which I need to get rid of, and it is structured as:

       

      {name: "'WORKER EXAMPLE"}

       

      In order to accomplish that, I used the operator $exists for the match. So this is what I used for performing the update:

       

      db.production_units.update({}, {$pull: {workers: {worker_number: {$exists: false}}}}, {multi: true})

      However it returned the following result:

      WriteResult({ "nMatched" : 30, "nUpserted" : 0, "nModified" : 0 })

       

      Which means that no nested document was matched for $pull.

      Yet, I tried an alternative command using a hack {$not: {$type: 16} in order to perform something alternative to {$exists: false} for the match, but it worked using the following update syntax:

       

      db.production_units.update({}, {$pull: {workers: {worker_number: {$not: {$type: 16}}}}}, {multi: true})

      Resulting in the following output:

      WriteResult({ "nMatched" : 30, "nUpserted" : 0, "nModified" : 16 })

       

      And afterwards, I realized that the nested document I wanted to delete, was removed.

      Show
      I'm trying to pull nested documents from a field of type array called " workers "  that is part of each document of a collection named "production_units", matching the condition that the nested document shouldn't contain 'worker_number' field. In fact I have a nested document which I need to get rid of, and it is structured as:   {name: "'WORKER EXAMPLE" }   In order to accomplish that, I used the operator $exists for the match. So this is what I used for performing the update:   db.production_units.update({}, {$pull: {workers: {worker_number: {$exists: false }}}}, {multi: true }) However it returned the following result: WriteResult({ "nMatched" : 30, "nUpserted" : 0, "nModified" : 0 })   Which means that no nested document was matched for $pull. Yet, I tried an alternative command using a hack {$not: {$type: 16} in order to perform something alternative to {$exists: false} for the match, but it worked using the following update syntax:   db.production_units.update({}, {$pull: {workers: {worker_number: {$not: {$type: 16}}}}}, {multi: true }) Resulting in the following output: WriteResult({ "nMatched" : 30, "nUpserted" : 0, "nModified" : 16 })   And afterwards, I realized that the nested document I wanted to delete, was removed.

      Problem

      The $exists operator is not working correctly for matching nested documents, using the syntax {field: {$exists: false}}.

      Further details

      This is a simplified version of production_units):

      [

          {{{}}

               "_id": ObjectId("5aca27b926974863ed9f01ab"),

              "name": "Z",

              "workers": [

                  {

                         "name": "X Y",

                         "worker_number": 655,

                         "employed": false,

                          "_id": ObjectId("5aca27bd26974863ed9f0425")

                {{    }}}

          ]

          }

      ]

            Assignee:
            daniel.hatcher@mongodb.com Danny Hatcher (Inactive)
            Reporter:
            kaisbe Kais Ben Salah
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: