5

I read in mongo db doc that I can use also LINQ but I don't understand some thing about it.

For example, if I write:

var result = collection.Find(filter);

and

var result = collection.AsQueryable()
              .Where(x => x.Foo == 114)

What is better?

LINQ filters on based of entire collection? Before I get entire collection and then it filters? Or before filters and it give me the collection already filtered?

4
  • 1
    ericlippert.com/2012/12/17/performance-rant Commented Jun 10, 2019 at 10:09
  • 1
    The LINQ should get converted into a Mongo query. Commented Jun 10, 2019 at 10:30
  • Linq is more readable for C# developer,but it cost some performance issue compare to MongoDB find command. I would prefer Find() command over linq for performance. Commented Apr 9, 2020 at 11:34
  • MongoDB Driver converts LINQ to Aggregation pipeline. For queries like you mentioned, I recommend you to use the first approach. Sometimes for really simple queries like Count() the difference between these will be huge. collection.Count(new BsonDocument()) is much faster than collection.AsQueryable().Count(). Commented Sep 16, 2022 at 20:26

1 Answer 1

7

Both do pretty much the same.

The LINQ API is a wrapper over the Collection API.

From briefly studying the sources of mongo-csharp-driver, I can see that the LINQ version calls either Collection.FindAs(...) or Collection.Distinct(...). It constructs the IMongoQuery passed in the query parameter, based on the LINQ expression. For that, it uses the query builder API (the Query class), such as Query.EQ

What is better?

It depends.

  • If you have types in C# code that represent structure of documents in the database, then you're likely to benefit from the LINQ API. You will benefit from strong typing, better readability, and you won't miss a renamed identifier whose name was passed as a string.
  • If you have dynamic structure of data, which has no concrete representation in code (you have some kind of metadata about a document, but not a class explicitly containing document properties). In such a case, using LINQ would be a struggle, and the raw Collection API would be a better choice.

LINQ filters on based of entire collection? Before I get entire collection and then it filters? Or before filters and it give me the collection already filtered?

Since LINQ API implements IQueryable and not just IEnumeable, all enumerable methods (such as Where or OrderBy) are not actually called, but rather are translated during compilation into code that builds expression trees. At runtime, the expression trees are built and passed to underlying query provider (implemented by MongoDB Driver in this case). The query provider translates expression trees into regular MongoDB queries and executes them through the regular MongoDB API.

See How to: Use Expression Trees to Build Dynamic Queries for more details.

So the query is actually executed in the database, and only the processed (filtered, ordered, or projected) results are returned to the application.

Yet, using LINQ API implies some performance overhead, because in addition to running the query, LINQ API also:

  • builds expression trees to pass them to query provider
  • visits expression trees to translate them into native query

In many cases this overhead is neglectable, but it depends on your requirements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.