[CSHARP-832] why does Explain() not work on Find set of methods Created: 28/Sep/13  Updated: 05/Apr/19  Resolved: 29/Sep/13

Status: Closed
Project: C# Driver
Component/s: Documentation
Affects Version/s: 1.8.2
Fix Version/s: None

Type: Task Priority: Minor - P4
Reporter: Ashutosh Assignee: Unassigned
Resolution: Done Votes: 0
Labels: driver, question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

var result = someCollection.AsQueryable<SomeType> ().Where(somecondition).Explain();

the above works where as

var result = someCollection.FindAs<SomeType> ().Where(somecondition).Explain();

will not

I would expect the explain to be available here as well or did I get something wrong ?



 Comments   
Comment by Craig Wilson [ 29/Sep/13 ]

Yes, you can get a look into it. The resulting type from FindAs is a MongoCursor (http://api.mongodb.org/csharp/1.1/html/48c6b240-3c2d-8d12-45e7-5b8b74605769.htm). Once you create a MongoCursor, you cannot further filter it's results. As such, you specify the query in the FindAs (or Find) call and can then set other things like skip (SetSkip), limit (SetLimit), or call Explain() on it.

The guidance is this: If you want to use LINQ, use AsQueryable. If you don't, then you either specify your queries by hand using a BsonDocument, or you can use the query builders in the MongoDB.Driver.Builders. For instance,

var query = Query.And(
  Query<Person>.GT(x => x.Age, 20),
  Query<Person>.EQ(x => x.LastName, "Smith"));
 
var result = collection.FindAs<Person>(query).SetSkip(2).SetLimit(20).Explain();

Comment by Ashutosh [ 29/Sep/13 ]

When are we supposed to use the Find based methods, is there some guidance on this ?

Can't we get a look in to the query that is executed in the second case, since I do expect some query to execute on the server, even if it is a poorly formulated one ?

Comment by Craig Wilson [ 29/Sep/13 ]

Your first example is a LINQ query and your second is not. Your second has the additional problem of pulling back all the document in the database and filtering on them locally, which is most definitely not what you want to do. So, if you want to use LINQ, you need to use the AsQueryable entry method.

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