MongoDb queries and system.linq

2019-07-17 21:09发布

问题:

When i use System.linq to query objects in a MongoCollection:

var result = collection.Find(query).Where(x => x.something == something);

is this a query done on the database or on the collection in memory?

for instance "SetSkip" creates the query in MongoDb but "Skip" does it in memory.

If ".Where" is done in memory is there a way not to do this?

回答1:

The .Where query is done in memory via IEnumerable.Where because it's performed on the result of the Find call that establishes the MongoDB query to perform.

To incorporate the .Where query into the Find, you can create a new query that ANDs the two queries together:

query = Query.And(query, Query<YourType>.EQ(x => x.something, something));
var result = collection.Find(query);