简体   繁体   中英

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

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

不知道如何将其转换为 C#,您需要的是$expr内的$expr $month$eq ,例如:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM