[SERVER-41354] Resultset change with 2x $or clauses at same level Created: 29/May/19  Updated: 11/Jun/19  Resolved: 29/May/19

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

Type: Bug Priority: Major - P3
Reporter: Andrea Bozzetto Assignee: Eric Sedor
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-6439 Duplicate fields at the same level sh... Backlog
Operating System: ALL
Steps To Reproduce:

Below the insert Script
 
This query returns two documents
 
db.getCollection('test').find({
   
'status': 'current'
 
,$or: [
{'identifier':  {$elemMatch:

{'system': 'urn:ihe:iti:xds:2013:accession', 'value': '1'}

}}
],
 
 $or: [
{'patientId.value':  {$in: [
'693561'
 
]}}
]
 
, 'type.coding.code': 'master'
})
 
Whereas 
this query returns only one document
 
db.getCollection('test').find({
    $or: [
{'patientId.value':  {$in: [
'693561'
 
]}}
],
'status': 'current'
 
,$or: [
{'identifier':  {$elemMatch:

{'system': 'urn:ihe:iti:xds:2013:accession', 'value': '1'}

}}
]
, 'type.coding.code': 'master'
})
 
Where is my mistake ?
 
 
Thanks a lot
 
 
*INSERT SCRIPT:
*
 
db.getCollection('test').insert({
    "status" : "current",
        "identifier" : [
           

{                 "use" : "temp",                 "system" : "[http://www.bmc.nl/zorgportal/identifiers/encounters]",                 "value" : "Encounter_Roel_20130404"             }

        ],
        "type" : [
            {
                "coding" : [
                   

{                         "system" : "[http://snomed.info/sct]",                         "code" : "11429006",                         "display" : "Master 123A"                     }

,
                   

{                         "system" : "[http://hl7.org/fhir/encounter-type]",                         "code" : "master",                         "display" : "master"                     }

                ]
            }
        ],
       
    "patientId" :

{         "value" : "693561"     }

}
);
db.getCollection('test').insert({
"status" : "current",
    "patientId" :

{         "value" : "693561"     }

,
        "identifier" : [
           

{                 "use" : "temp",                 "system" : "urn:ihe:iti:xds:2013:accession",                 "value" : "1"             }

        ],
        "type" : [
            {
                "coding" : [
                   

{                         "system" : "[http://snomed.info/sct]",                         "code" : "11429006",                         "display" : "Consultation"                     }

,
                   

{                         "system" : "[http://hl7.org/fhir/encounter-type]",                         "code" : "master",                         "display" : "master"                     }

                ]
            }
        ]
}
);

Participants:

 Description   

Hi,
   I have a problem with $or clause.
 
I create  two documents in a test collection. 
When I search a document with a $or clause at the end of the query, It returns two documents, whereas if I move $or as first clause the query returns only one document
 
 



 Comments   
Comment by Eric Sedor [ 29/May/19 ]

I believe the expected way to write this $or query is:

{
	"status" : "current",
	"$or" : [
		{
			"identifier" : {
				"$elemMatch" : {
					"system" : "urn:ihe:iti:xds:2013:accession",
					"value" : "1"
				}
			}
		},
		{
			"patientId.value" : {
				"$in" : [
					"693561"
				]
			}
		}
	],
	"type.coding.code" : "master"
}

I may have misunderstood the logic you are encoding, but the important part of my suggestion is that only one $or operator can be provided at any particular depth of the query document (the top level in this case). The unpredictable behavior you are seeing is due to one $or clause overwriting the other at the top level. You would see similar unpredictability if you queried for db.test.find({"status" : "current", "status":"other"})

SERVER-6439 is tracking a request to disallow this sort of document, so I am closing this ticket as a duplicate of that issue.

For further discussion about how to use specific MongoDB features, please ask our community by posting on the mongodb-user group or on Stack Overflow with the mongodb tag. The SERVER project is for reporting bugs and feature requests.

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