[英]How to filter datetime.Month from mongo collection by aggregation?
I have a Mongo collection that contains a dateTime field called "DateOfCreation" .我有一个Mongo 集合,其中包含一个名为"DateOfCreation"的日期时间字段。 Given that I have a specific number of month, how can I get all documents that matches to month of the DateOfCreation field?
鉴于我有特定的月份数,如何获取与DateOfCreation字段的月份匹配的所有文档?
Below, is what I tried to do, but it does not work and returns 0 Documents.下面是我尝试做的,但它不起作用并返回 0 个文档。
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) 3 - 是我的月数(三月)
Similarly, I would like to do it for year , day and so on同样,我想为year , day等等做
不知道如何将其转换为 C#,您需要的是$expr
内的$expr
$month
和$eq
,例如:
db.collection.aggregate([{$match:{$expr:{$eq:[{$month:"$DateOfCreation"},3]}}}])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.