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.
Example:
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@10.10.11.11:27017/ampleServiceQA";
var Client = new MongoClient(conString);
var DB = Client.GetDatabase("ampleServiceQA");
var query
= Query<ample>.NotExists(u
=> u.style.number);
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)
{
Console.WriteLine(doc);
}
Console.ReadLine();
}
public class ample
{
public Style style { get; set; }
}
public class Style
{
public string description { get; set; }
public string number { get; set; }
}
}
}