[SERVER-10697] Having multiple $or clauses in a query results in the clauses being merged together Created: 06/Sep/13  Updated: 06/Apr/23  Resolved: 06/Sep/13

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

Type: Question Priority: Major - P3
Reporter: Michael Barszcz Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

RHEL 6.1


Participants:

 Description   

My understanding is that the default behavior when querying is for an implicit AND between fields being queried. But in the case of a query with multiple $or clauses the effect is for the clauses to be merged into one $or statement. So a query like:
{ $or : [

{ event : "e1" }

,

{ event : "e2" }

], $or [

{ host : "h1" }

,

{ host : "h2" }

] }

I would have expected results containing only events e1 or e2 that are hosted by h1 or h2. But the actual results are document with events e1 or e2 or host h1 or h2. Is this the expected behavior or should the query hold true to the implicit AND between fields?

NOTE: I can get the results I want by nesting the $or clauses within an $and.



 Comments   
Comment by Michael Barszcz [ 09/Sep/13 ]

Well I can agree that it's javascript in the case of the shell, but for other languages (C++ driver is where I came across this), I can argue it's something the library has control over. When I build my BSON object to use in the query I can print out the object and it will show two $or statements at the same level in my query. It's only when that BSON Object is parsed do the $or clauses get merged together. I don't see why that parsing function wouldn't be able to check for special fields like an operator. I understand it may not be worth it to implement, but it should be possible.

Comment by Scott Hernandez (Inactive) [ 06/Sep/13 ]

Unfortunately that is the language (javascript) doing it. Other languages have the same problems, like java, python, ruby, php, .net, ....

Comment by Michael Barszcz [ 06/Sep/13 ]

Okay that's what I figured was the cause. I wasn't sure if operators should be treated different. But I will use the $and operator to get the results I need.

Comment by Scott Hernandez (Inactive) [ 06/Sep/13 ]

In the (javascript) shell, and most languages, you cannot have duplicate field names at the same level.

> var q = { $or : [ { event : "e1" }, { event : "e2" } ], $or: [ { host : "h1" }, { host : "h2" } ] }
> q
{ "$or" : [ { "host" : "h1" }, { "host" : "h2" } ] }

If you want to do this you will need to use $and to support duplicate field names at the same level of your query.

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