[CSHARP-922] LINQ queries with coll.Any() can be improved to avoid $elemMatch. Created: 05/Mar/14  Updated: 20/Jan/16  Resolved: 08/May/15

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 1.8.3
Fix Version/s: 2.1

Type: Improvement Priority: Minor - P4
Reporter: Michael Kennedy Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: Rewrite Linq
Backwards Compatibility: Fully Compatible

 Description   

Presently any query of the form:

coll.Where(c => c.Array.Any(a => a.value == val))

maps to the database as something like:

db.Coll.find({ "Array" : { "$elemMatch" :

{"Value" : val }

} } )

A faster query which is entirely acheivable in the C# driver is:

db.Coll.find(

{ "Array.Value" : val }

)

For example, this query:

books.Where(b => b.Ratings.Any(r => r.Value >= 0))

becomes:

db.Book.find({ "Ratings" : { "$elemMatch" : {"Value" :

{ "$gte" : 0 }

} } } )

It should be

db.Book.find({ "Ratings.Value":

{ "$gte" : 0 }

} )



 Comments   
Comment by Githook User [ 08/May/15 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-922: single condition Any calls in LINQ no longer use $elemMatch.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/74c463443ca04e0d39cbc1deacbb59a638972fc0

Comment by Michael Kennedy [ 05/Mar/14 ]

Sorry I forgot to add this when I submitted:

I tested this both on 2.4.9 and 2.6.0_rc0.

Generated at Wed Feb 07 21:38:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.