MongoDB with FilterDefinition in C#

MongoDB with C#



First you need to install "MongoDB.Driver" from nuget.

Code


MongoClient client;
           MongoUrl url = new MongoUrl("mongodb://localhost:PortNumber/databasename");
           client = new MongoClient(url);
 
           var database = client.GetDatabase(url.DatabaseName);
 
           FilterDefinition<CollectionClassName> filterQuery;
           var builder = Builders<CollectionClassName>.Filter;
 
           filterQuery = builder.And(
               builder.Eq("FieldName", NumericValue),
               builder.Eq("Field2Name""StringValue")
               );
 
           IMongoCollection<CollectionClassName> collectionResult = database.GetCollection<CollectionClassName
>("collectionName");
           var result = collectionResult.Find(filterQuery).ToList();

  
Hope this will help you and save your time.

Enjoy !!!

:)

No comments:

Post a Comment