[SERVER-14208] Negated match with $or not working Created: 09/Jun/14  Updated: 09/Jun/14  Resolved: 09/Jun/14

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

Type: Bug Priority: Major - P3
Reporter: Neil Lunn Assignee: Stennie Steneker (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

Given the documents

{ "status" : "A", "type" : "C" }
{ "status" : "A", "type" : "D" }
{ "status" : "B", "type" : "C" }
{ "status" : "B", "type" : "D" }

Issuing a query like this:

db.collection.find({
    "$or": [
        { "status": { "$ne": "A" } }, 
        { "type": { "$ne": "C" }},
    ]
})

Returns all documents, when the expected result would be to exclude the first document.

Participants:

 Description   

There is a different behavior of a given query using inequality matching in combination with $or.

There is some additional information on consideration of $not as a top level operator in SERVER-14201



 Comments   
Comment by Neil Lunn [ 09/Jun/14 ]

Typo in the field name for the data that was missed caused the issue. Good to close.

Comment by Stennie Steneker (Inactive) [ 09/Jun/14 ]

Hi Neil,

Unless I'm missing something, this seems to return results as expected in 2.6.1.

Set up:

db.collection.insert([
{ "status" : "A", "type" : "C" },
{ "status" : "A", "type" : "D" },
{ "status" : "B", "type" : "C" },
{ "status" : "B", "type" : "D" }
]);

Query:

db.collection.find({
    "$or": [
        { "status": { "$ne": "A" } }, 
        { "type": { "$ne": "C" }},
    ]
})

Results:

{
	"_id" : ObjectId("53959c5d69a853f1f3af6a93"),
	"status" : "A",
	"type" : "D"
}
{
	"_id" : ObjectId("53959c5d69a853f1f3af6a94"),
	"status" : "B",
	"type" : "C"
}
{
	"_id" : ObjectId("53959c5d69a853f1f3af6a95"),
	"status" : "B",
	"type" : "D"
}

Do you have any other indexes on your test collection? Are you definitely seeing different results?

Regards,
Stephen

Generated at Thu Feb 08 03:34:10 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.