Hi, Konrad,
I’m one of the C# folks around these parts. We introduced support for DayOfWeek
in our new LINQ3 provider. Internally we use the LINQ provider both for LINQ support and to translate lambdas in Fluent Find, Fluent Aggregate, and elsewhere into their MQL equivalents.
using System;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
var settings = new MongoClientSettings { LinqProvider = LinqProvider.V3 };
var client = new MongoClient(settings);
var db = client.GetDatabase("test");
var coll = db.GetCollection<Appointment>("coll");
var find = coll.Find(x => x.Date.DayOfWeek != DayOfWeek.Sunday);
Console.WriteLine(find);
var query = coll.AsQueryable().Where(x => x.Date.DayOfWeek != DayOfWeek.Sunday);
Console.WriteLine(query);
class Appointment
{
public ObjectId Id { get; set; }
public DateTime Date { get; set; }
}
This will output the following MQL:
find({ "$expr" : { "$ne" : [{ "$subtract" : [{ "$dayOfWeek" : "$Date" }, 1] }, 0] } })
test.coll.Aggregate([{ "$match" : { "$expr" : { "$ne" : [{ "$subtract" : [{ "$dayOfWeek" : "$Date" }, 1] }, 0] } } }])
Note that LINQ3 was introduced in MongoDB .NET/C# Driver 2.14.0 (latest is 2.14.1) and is opt-in at the moment. More details can be found in our LINQ3 docs.
In summary, once you opt into LINQ3 you should be able to use DateTime.DayOfWeek
in lambda expressions for LINQ, Fluent Find, Fluent Aggregate, and elsewhere and the driver will express this in MQL as $dayOfWeek
.
Sincerely,
James