[SERVER-38497] The $exists operator is not working correctly Created: 10/Dec/18  Updated: 10/Dec/18  Resolved: 10/Dec/18

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 4.0.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Kais Ben Salah Assignee: Danny Hatcher (Inactive)
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

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.

Participants:

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

          {{    }}}

    ]

    }

]



 Comments   
Comment by Danny Hatcher (Inactive) [ 10/Dec/18 ]

Hello,

Because neither of us are able to reproduce this issue, I will close this ticket. If you are able to encounter this problem in a reliable manner, please let us know on this ticket.

In general, it is a good idea to reach out on the mongodb-user group or Stack Overflow with the mongodb tag for discussions like this. Quite often the community has run into the same problems before and know a way to fix it.

Thank you,

Danny

Comment by Kais Ben Salah [ 10/Dec/18 ]

Hello Daniel,

I re-tried to push the document I described above, and repeated the update command, however it has worked fine.

The test case you provided worked for me as well.

Honestly I don't know why it didn't work the previous time - I'm unable to determine the cause of the issue.

Comment by Danny Hatcher (Inactive) [ 10/Dec/18 ]

Hello,

On my local version of 4.0.4 I believe I was able to replicate your scenario but everything worked as expected. Can you please let me know the difference between my test and yours?

> db.bar.findOne()
{
	"_id" : ObjectId("5c0e8bc36d37020bcb73d07e"),
	"a" : 1,
	"b" : [
		{
			"c" : "string",
			"d" : 1,
			"e" : false
		}
	]
}
> db.bar.update({}, {$pull: {b: {d: {$exists: false}}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.bar.findOne()
{
	"_id" : ObjectId("5c0e8bc36d37020bcb73d07e"),
	"a" : 1,
	"b" : [
		{
			"c" : "string",
			"d" : 1,
			"e" : false
		}
	]
}
> db.bar.update({}, {$pull: {b: {f: {$exists: false}}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.bar.findOne()
{ "_id" : ObjectId("5c0e8bc36d37020bcb73d07e"), "a" : 1, "b" : [ ] }

Thank you,

Danny

Generated at Thu Feb 08 04:49:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.