I have a sql query I need to make equivalent mongodb
query I am using c#
driver. Is it possible to create a dynamic method to create mongo equivalent of these kind of sql
queries where I can specify dynamically what all columns should be taken with dynamically created where conditions. Please help to create mongo equivalent query using c#
SELECT distinct(employeeID), firstname, Lastname, salary, joinedOn FROM [Employees]
where salary >= 40000 and joinedOn between '2011/02/25' and '2012/02/27'
What about use Linq to build a dynamic query?
public T findOne<T> (Expression<Func<T, bool>> expression) where T: class
{
var db = server.GetDatabase(databaseName);
var documents = db.GetCollection<T>(typeof(T).Name);
return (documents.AsQueryable<T>().Where(expression)).SingleOrDefault;
}
You can use it in this way:
var employee = findOne<Employee>(e=> d.salary>=4000 &&
d.joinedOn > "2011/02/25" &&
d.joinedOn < "2012/02/27");
I think you should convert joinedOn to DateTime.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.