Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo C# driver - Building filter dynamically with nesting

Tags:

c#

mongodb

Assume something like a nested expression

where a = 1 AND ( b = 4 OR b = 5 )

How to use multiple Builders objects and build a complete filter out of such an expression?

like image 704
Mr767267 Avatar asked Aug 26 '15 12:08

Mr767267


People also ask

What is Mongo C?

A Cross Platform MongoDB Client Library for C. 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 a Mongo driver?

The official MongoDB Node. js driver allows Node. js applications to connect to MongoDB and work with data. The driver features an asynchronous API which allows you to interact with MongoDB using Promises or via traditional callbacks.


1 Answers

Builders is really flexible class, it also has overridden operators "& = AND" and "| = OR"

Your example will be

var filter = Builders<User>.Filter.Eq(x => x.A, "1"); filter &= (Builders<User>.Filter.Eq(x => x.B, "4") | Builders<User>.Filter.Eq(x => x.B, "5")); 
like image 51
rnofenko Avatar answered Oct 15 '22 01:10

rnofenko