Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-922

LINQ queries with coll.Any() can be improved to avoid $elemMatch.

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor - P4
    • Resolution: Fixed
    • Affects Version/s: 1.8.3
    • Fix Version/s: 2.1
    • Component/s: Linq
    • Labels:
      None
    • Sprint:
      C# Sprint 19

      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 }

      } )

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: