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

The $exists operator is not working correctly

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Cannot Reproduce
    • Affects Version/s: 4.0.4
    • Fix Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • Operating System:
      ALL
    • Steps To Reproduce:
      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.

      Description

      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")

                {{    }}}

          ]

          }

      ]

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: