23

First of all I'm new to MongoDb. In MongoDb C# driver 1.9.x, i can take collections as queryable with AsQueryable method like this.

        var db = client.GetServer().GetDatabase("test");
        var col = db.GetCollection("Video");
        var qrlist = col.AsQueryable();

I installed new driver 2.0rc and while using it, i cannot use AsQueryable method because it is missing. Is it departed or is there another way to accomplish this? (I have already included MongoDB.Driver.Linq).

        var db = client.GetDatabase("test");
        var col = db.GetCollection<Contact>("Contact"); //GetCollection without <T> is missing to.
        var qrlist = col.AsQueryable(); // AsQueryable missing here.

How can i get my entities as queryable in new driver, need help from MongoDb gurus. Thank you.

ilker unal
  • 502
  • 1
  • 6
  • 15

3 Answers3

25

19 October update:

MongoDB 2.1 driver is out https://github.com/mongodb/mongo-csharp-driver/releases/tag/v2.1.0

It supports LINQ:

LINQ

CSHARP-935 LINQ support has been rewritten and now targets the aggregation framework. It is a more natural translation and enables many features of LINQ that were previously not able to be translated.

Simply use the new AsQueryable method to work with LINQ.

18 September update:

MongoDB 2.1 driver RC should support it. See https://jira.mongodb.org/browse/CSHARP-935

Finally 2.1 rc came out. Great work!

Old answer:

No, AsQueryable is unsupported: https://jira.mongodb.org/browse/CSHARP-935

Type: Epic Status:OPEN Priority: Major - P3 Resolution: Unresolved

And from the hourse's mouth: Craig Wilson on the google forum

Yes, there is currently no AsQueryable on the new api. You can track this feature here (https://jira.mongodb.org/browse/CSHARP-935). We simply didn't have enough time to get it completed and tested thoroughly. It is scheduled for 2.1 and is a priority for us. Until then, we have integrated expression tree functionality into the Find and Aggregate methods (and some of the write methods for filtering) such that you may not need a full LINQ implementation. For instance, see the sample test class here as an example: https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver.Tests/Samples/AggregationSample.cs#L77

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • 1
    Sorry to hear that. Thank you for your answer Xanatos. – ilker unal Mar 18 '15 at 15:40
  • 2
    @user1562523 I think much refactoring will be needed to use the newer drivers. – xanatos Mar 18 '15 at 15:43
  • 1
    This issue was updated yesterday in the mongo jira, so we should be seeing support for linq again soon. – SomeInternetGuy Sep 18 '15 at 14:01
  • Great news. Thank you. – ilker unal Oct 02 '15 at 08:02
  • .AsQueryable seems to be available now with MongoDB.Driver 2.4.3 I was able to add a using for "MongoDB.Driver.Linq" and then I was able to access my collection using IQueryables using the following syntax: `_collection.AsQueryable().Where(predicate);` oh - i notice this is explained above, I just read the comment wrong. – Kris Coleman Apr 18 '17 at 19:05
5

I originally had the following using mongocsharp version 1.9x:

public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
{
    return _collection.AsQueryable<T>()
                      .Where(predicate.Compile()).AsQueryable();
}

Was able to get the same results in version 2 using:

public async Task<List<T>> SearchFor(Expression<Func<T, bool>> predicate)
{
    return await _collection.Find(Builders<T>.Filter.Where(predicate)).ToListAsync();
}

Hope it helps.

nikopol65
  • 51
  • 3
  • Thank you for your answer. Unfortunately getting data as a list is a big performance problem for me. – ilker unal Apr 25 '15 at 12:50
  • ToListAsync is expensive. You're violating the purpose of IQueryable because you're forcing an eager retrieval of **all** documents in the collection that match. Callers can't further filter unless they do it in memory. IQueryable is supported now by MongoDB.Driver, so you could return IQueryable and use `_collection.AsQueryable().Where(predicate)`. Also, in your first example, you have "AsQueryable()" twice in your linq statement. The .Where already returns as IQueryable so the last AsQueryable() in the first example is redundant and unnecessary. – Kris Coleman Apr 18 '17 at 19:10
1

While other answers indicated that the early version 2 releases of the drivers didn't include the AsQueryable this is now available in the latest version of the drivers (I've not checked exactly which version introduced it).

The method is found in MongoDB.Driver.IMongoCollectionExtensions and can be invoked as you would expect. That is:

IMongoCollection<TDocument> collection = ...;
IMongoQueryable<TDocument> queryable = collection.AsQueryable();
Chris
  • 27,210
  • 6
  • 71
  • 92