Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB C# .NET Driver ASP.NET Core Unsupported Filter

I am having trouble making my filters work with MongoDB .NET Driver, I get this error:

Unsupported filter: Invoke(value(System.Func2 [Role,System.Boolean]), {document}{Model}).

When trying to run this code:

public virtual async Task<PartitionedModel<T>> GetByAsync(Func<T, bool> filter)
{
    Expression<Func<PartitionedModel<T>, bool>> filt = (i) => filter(i.Model);
    PartitionedModel<T> item = (await collection.FindAsync(filt)).FirstOrDefault();
    return item;
}

and the class PartitionedModel look like this:

public class PartitionedModel<T> where T : IModel
{
    public ObjectId Id { get; set; }
    public PartitionOffset PartitionOffset { get; set; }
    public T Model { get; set; }
}

I did a refacto of my code working from having collections handling IModel directly to working with PartitionedModel which is a holding class for my IModel, the GetByAsync function worked correctly before I subclassed IModel

I found little to no informations about this problem except this: Dynamic Linq Predicate throws "Unsupported Filter" error with C# MongoDB Driver

But it seems like my version of the MongoDB C# Driver doesnt accept Func<> in parameter as filter, I can only pass Builder<> or Expression<> as a filter into the Find functions

Can someone enlighten me a bit about this error ?

EDIT:

I tried to run this code by replacing the FindAsync(filt) with FindAsync(_ => true) and it actually works

Also, here is the code that is used to retrieve the collection

protected readonly IMongoCollection<PartitionedModel<T>> collection;

public GenericRepository(IMongoDatabase dbContext, string collectionName)
{
    collection = dbContext.GetCollection<PartitionedModel<T>>(collectionName);
}

and the version of my driver seems to be 2.7.0 driver version

EDIT 2: I've made my query works using this:

PartitionedModel<T> item = collection.AsQueryable().FirstOrDefault(filt);

But i'm not sure what is the implication of using a non-async version, can anyone tell me if this is wrong or if this will be a problem ?

like image 243
Rakiah Avatar asked Dec 12 '18 10:12

Rakiah


People also ask

What is MongoDB C driver?

The MongoDB C Driver, also known as “libmongoc”, is a library for using MongoDB from C applications, and for writing MongoDB drivers in higher-level languages. It depends on libbson to generate and parse BSON documents, the native data format of MongoDB.

Is MongoDB a C++?

Welcome to the documentation site for the official MongoDB C++ driver. You can add the driver to your application to work with MongoDB using the C++11 or later standard.

Is MongoDB free to use?

MongoDB Community is the source available and free to use edition of MongoDB. MongoDB Enterprise is available as part of the MongoDB Enterprise Advanced subscription and includes comprehensive support for your MongoDB deployment.

What is Mongo-C in C?

MongoDB C-driver, formerly known as mongo-c, is a project comprising two libraries: libmongoc, a client library that has been written in C for MongoDB. libbson, a library that offers practical routines related to building, parsing, and iterating BSON documents, the native data format of MongoDB.

What is the MongoDB C driver?

The MongoDB C Driver, also known as "libmongoc", is the official client library for C applications, and provides a base for MongoDB drivers in higher-level languages. The library is compatible with all major platforms.

What is libmongoc in MongoDB?

The MongoDB C Driver, also known as “libmongoc”, is a library for using MongoDB from C applications, and for writing MongoDB drivers in higher-level languages. It depends on libbson to generate and parse BSON documents, the native data format of MongoDB.

What is MongoDB used for?

MongoDB is developed by MongoDB Inc. and is published as free and open-source software. A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents.


1 Answers

Seems like current implementation of c# mongo driver is not supported delegate-based filters.

https://github.com/mongodb/mongo-csharp-driver/blob/da0cff54c67208d979b030abb160f958d3276925/src/MongoDB.Driver/Linq/Translators/PredicateTranslator.cs#L76

The switch doesn't contain a ExpressionType.Invoke (the type of expression in delegate-based filter) case.

like image 133
Stas Kuzminoff Avatar answered Oct 01 '22 12:10

Stas Kuzminoff