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

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

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.1
    • Affects Version/s: 1.8.3
    • Component/s: Linq
    • Labels:
      None
    • Fully Compatible

      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 }

      } )

            Assignee:
            craig.wilson@mongodb.com Craig Wilson
            Reporter:
            mkennedy66996693 Michael Kennedy
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: