Hi ,

I am not able to pass filters into Find method , Find method accepts only query…

Please help me on this.

results = db.collection.Find(Filter)

Hello @mubarak_shaik, Welcome to the MongoDB community forum,

What kind of filters are you talking about?

Can you please provide more details, with the example?

Hi @turivishal ,

thanks for your reply.

i am using something like below

var builder = Builders.Filter;
var query = builder.Eq(“columname”, “test”);

var results = _database.GetCollection(“Model”).Find(query).ToList();

Find is accepting only , not builder…how to manage this.

I am not sure what language is this, Can you please provide more details, on what language is? nodejs, c#, etc. so related person can help you.

Hi ,

it is c#.net …

your example above seems ok but it is possible you forget to define the type of builder. most examples I see use Builders<BsonDocument>

I recommend checking Quick Tour (mongodb.github.io) for C# driver. It starts using Filter about the half of the page after “Get a Single Document with a Filter” section.

check the following for more builder examples (filter, projection, sort)

Hi ,

I am using below code snippet.

var builder = Builders<Model>.Filter;
var query = builder.Eq(“Columnname”, “test”);

var res = _database.GetCollection<Model>(“CollectionName”).Find(query).ToList();

i see there …Find will accept only ImongoQuery…

Hi ,

please some one can help on this.

I am failing to understand the problem here. When you use Builders and make a filter, you just use it as your query.

Can you please share what error is that you are getting?

Hi ,

this is the error , it is not accepting as query…

I have 2 suspicions about the problem: Your class definition has a problem, or somehow you are using a different namespace for one of your variables. Can you please check them first?

I have run a simple test right now and it just works (uses new C# top-level coding style):

Example Code
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;

var URI = $"mongodb+srv://YOUR_CONNECTION_URI/?retryWrites=true&w=majority";

// Create a document using BSON
    MongoClient dbClient = new MongoClient(URI);
    var database = dbClient.GetDatabase("testme");
    var collection = database.GetCollection<BsonDocument>("class");
    var document = new BsonDocument { { "id", 1 }, { "class", "1A" } };
    await collection.InsertOneAsync(document);

// Find & Read documents with Filter and Model class
    MongoClient dbClient = new MongoClient(URI);
    var database = dbClient.GetDatabase("testme");
    var collection = database.GetCollection<Model>("class");

    var builder = Builders<Model>.Filter;
    var filter = builder.Eq(model => model.Class, "1A");
    var documents = collection.Find(filter);
    foreach (var doc in documents.ToList())
        Console.WriteLine($"id: {doc.SId} class: {doc.Class}");

class Model
    public string? Id { get; set; }
    public int SId { get; set; }
    public string Class { get; set; } = null!;

I have found the code you referenced and it seems you are using a legacy driver: MongoDB.Driver.Legacy/MongoCollection.cs

this changes things. can you please tell us which versions of C# and MongoDB driver you use?

I may not be able to cop with the versions you use, so please accept my apology if I stay silent long. It is still plausible if you upgrade your version and the code should work out nicely.

Check this link if you have to use legacy driver : Getting Started - v1.11

instead of Builders, filtering is done with Query class

var query = Query<Entity>.EQ(e => e.Id, id);
var entity = collection.FindOne(query);

Hi @Yilmaz_Durmaz ,

This is working fine , i was searching builder instead query. But query is working expected good.

Hi @Yilmaz_Durmaz ,

I am using below 2.18 drivers with latest dotnet framework 4.8 with c# 10.0 .

Don’t trust what you “see” as installed. It is about what you “use”.

Check your code everywhere for a sign of MongoDB.Driver.Legacy first, and also check for MongoDB.Driver version lower than 2.xx (can be 1.11).

I am insisting on these terms because of these reasons:

  • this following code which your screen shot refers exists only in Legacy driver code:

    • public virtual MongoCursor<TDefaultDocument> Find(IMongoQuery query)
  • You are clearly state that Query works (which exists in Legacy code), Builders does not (which is in versions >=2.0).

  • 2.0 version upgrade notes tell that Query and others are replaced by Builders

It is quite possible Visual Studio changed the used library to 1.xx version (or imported the wrong namespace) when you first tried to use Query (which is not part of 2.xx) as you possibly clicked auto-complete without knowing the result. This happens quite frequently to all of us and we mostly recognize the error only when things get pretty ugly and annoying.

Hi ,

Thanks for your instruction.

First of all i have not used so far 1.x version.

Now i am using 2.18 version and can confirm further , but still need your help on any clue.

I don’t have Visual Studio and having a hard time finding out how to replicate your issue with dotnet 6.0

there are two packages in nuget repository; “mongocsharpdriver” for the legacy and “MongoDB.Driver” for the new. Both are seemingly at version 2.18, installing the same named dll files, thus making it hard to distinguish.

If your project is not a big fat one, can you start a new one and copy source files (settings too if needed) Then add other libraries as usual but use “MongoDB.Driver” to install.

Or first try removing all MongoDB packages from your project, and find and install “MongoDB.Driver” package, not “mongocsharpdriver” package.

Alright, I finally replicated your issue and got to the culprit code causing it. Below is the example code for both Legacy and 2.xx versions. You can check this and the other code I gave above for differences to modernize your code (or at least use it as a starter).

Legacy and 2.xx query/filter examples
// Legacy
    MongoClient dbClient = new MongoClient(URI);

    var databaseL = dbClient.GetServer().GetDatabase("testme");
    var query = Query<Model>.EQ(model => model.SId, 2);
    var collectionL = databaseL.GetCollection<Model>("grades");
    var documentsL = collectionL.Find(query);
    foreach(var doc in documentsL.ToList()){Console.WriteLine(doc.SId);};
// 2.xx
    MongoClient dbClient = new MongoClient(URI);

    var database = dbClient.GetDatabase("testme");
    var collection = database.GetCollection<Model>("grades");
    var builder = Builders<Model>.Filter;
    var filter = builder.Eq(model => model.SId, 2);
    var documents = collection.Find(filter);
    foreach(var doc in documents.ToList()){Console.WriteLine(doc.SId);};

The problem is caused by the way you access the database. This is the modern way:

var database = dbClient.GetDatabase("testme");

And the following is the legacy code to do the same:

var databaseL = dbClient.GetServer().GetDatabase("testme");

A very simple, easy to miss, extra code to remove: GetServer().

Keep an eye out for similar code fragments in your journey ahead.

PS: Working with legacy code is not easy. Yet it teaches new experiences. Took me a day but I consider it a well-spent time.