Skip to main content

OR in MongoDB with C# 2.2.4 driver

OR condition in MongoDB is used to check whether any of the given conditions matches. It is indicated with “|” symbol. In below Example we are fetching the records from amplecollection for which ‘Style’ key doesn’t exist or ‘style’ key doesn’t have any value.


using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Shared;
using MongoDB.Driver.Builders;

namespace MCNotexists
    public class Program
      static void Main(string[] args)
         string conString = "mongodb://ampleServiceQA:ampleServiceQAPassword@";
         var Client = new MongoClient(conString);
         var DB = Client.GetDatabase("ampleServiceQA");
         var query = Query<ample>.NotExists(u =>;
         var builder = Builders<BsonDocument>.Filter;
         var collection = DB.GetCollection<BsonDocument>("amplecollection");
         var filter = builder.Exists("style", false) | builder.Eq("style", "");
         var RetrievedData = collection.Find(filter).ToList();
         foreach (var doc in RetrievedData)

        public class ample
            public Style style { getset; }
        public class Style
            public string description { getset; }
            public string number { getset; }


Output : fetches the records from amplecollection for which ‘Style’ key doesn’t exist or ‘style’ key doesn’t have any value