[CSHARP-461] Create Linq + MongoCursor query generation Created: 03/May/12 Updated: 20/Mar/14 Resolved: 03/May/12 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 1.4.2 |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Brian | Assignee: | Robert Stam |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
Problem: Execution: Expected: |
| Comments |
| Comment by Craig Wilson [ 09/Feb/13 ] | |||||||||||
|
We are doing some similar things like explain, withindex, and batch size. It wouldn't be difficult to put in, but it would get deprecated relatively quickly which presents the problem. We don't really want to introduce something for a release only to pull it the next release. But it's a good suggestion... We'll discuss it. | |||||||||||
| Comment by Daniel Sinclair [ 09/Feb/13 ] | |||||||||||
|
It wouldn't be elegant, but wouldn't this be more quickly introduced for the short-term by putting a call to SetFields inside MongoDB.Driver.Linq.SelectQuery.Execute() ? Similar to _take and _limit. Obviously you'd need a hacky way to configure those fields, probably an AsQueryable overload. Alternatively, what about some way of injecting properties onto the cursor that the Linq query will end up using? | |||||||||||
| Comment by Craig Wilson [ 08/Feb/13 ] | |||||||||||
|
We don't have control over the "AsQueryable" coming off the MongoCursor as that is a System.Linq extension method. We used AsQueryable for our method name on MongoCollection because it is the generally accepted name. In version 2.0, we are looking to make some changes around MongoCursor to hopefully alleviate this problem. Currently, if you are looking to trim the field list down, you simply can't use linq. That seems to be your impetus for your request. However, we are working on rewriting the linq provider and part of the rewrite is to bake support in for field limiting. You can track the progress in | |||||||||||
| Comment by Daniel Sinclair [ 08/Feb/13 ] | |||||||||||
|
How do you do field restriction with linq? Or how can I get one of these IQueryable interfaces from a MongoCursor? I ran into this problem and it's been baffling me for the last 36 hours why my queries aren't going through to the database. Again, I was calling System.Linq.Queryable.AsQueryable() on the MongoCursor rather than MongoDB.Driver.Linq.LinqExtensionMethods.AsQueryable() on the MongoCollection. This is a real gotcha as I think I've made this mistake before. It's easily done as they're both identically named extension methods and since they both return an IQueryable<>. I think it's an unfortunate confusion, and I'd rather have seen it named something else, but I just need to make a note somewhere not to fall into this trap again. Unfortunately, you can't easily guard against it if you have both namespaces included in your file. I might just try being explicit (var iq = MongoDB.Driver.Linq.LinqExtensionMethods.AsQueryable(col)). So the reason I was using MongoCursor in the first place was so I can use the .Fields property. AsQueryable() was then the trap I laid for myself. So is it possible to combine a cursor with the linq methods? Or can use do field restriction with the linq model? | |||||||||||
| Comment by Brian [ 03/May/12 ] | |||||||||||
|
Combininig could be good in case when you need to create some search engine. With Query methods it could be done easily but then you want to apply some common conditions but you don't want to use "magic string" to do this. So you combine your query with Linq. Thank you. Inject is that what I need. | |||||||||||
| Comment by Robert Stam [ 03/May/12 ] | |||||||||||
|
The Find method of MongoCollection<T> returns a MongoCursor<T> which in turn implements IEnumerable<T>, at which point you are committed to evaluating the rest of the LINQ queries client side. Usually you would write your query entirely in LINQ. It is unusual to mix a regular MongoDB query with a LINQ query. You probably should write your entire query in LINQ, but if you really want to mix the models you can use the Inject method to inject the lower level MongoDB query into the higher level LINQ query, as in:
Note: to use LINQ to Mongo you call AsQueryable on the collection, not on the cursor. |