How to filter datetime.Month from mongo collection by aggregation?

Viewed 147

I have a Mongo collection that contains a dateTime field called "DateOfCreation". Given that I have a specific number of month, how can I get all documents that matches to month of the DateOfCreation field?

Below, is what I tried to do, but it does not work and returns 0 Documents.

var documents = Collection.Aggregate()
     .Match(new BsonDocument("DateOfCreation", new BsonDocument("$month", new BsonDocument("$eq", 3))))
     .Group(groupFields)
     .ToList();

3 - is my number of month (march)

Similarly, I would like to do it for year, day and so on

1 Answers

Not sure how to translate this to C#, what you need is $month inside of $expr, with $eq, like:

db.collection.aggregate([{$match:{$expr:{$eq:[{$month:"$DateOfCreation"},3]}}}])
Related