【发布时间】:2021-09-24 11:03:27
【问题描述】:
我有以下 mongoDB 查询在 mongoDB shell 中运行良好,但想知道如何在 C# 中使用该查询?
db.collection.aggregate([{
$match: {
fieldName: "dsdsd",
createdAt: {
$gte: ISODate("2021-07-05T12:29:30.000+00:00"),
$lte: ISODate("2021-07-15T12:29:30.000+00:00")
}
}
}, {
$group: {
_id: {
$dateToString: {
format: "%Y-%m-%d-%H",
date: "$createdAt"
}
},
items: {
$first: '$$ROOT'
}
}
},{"$replaceRoot":{"newRoot":"$items"}}
,{"$sort":{"createdAt":-1}}
])
我想在 c# 中使用以下原始查询,如下所示:
var pipeline = {
$match: {
fieldName: "dsdsd",
createdAt: {
$gte: ISODate("2021-07-05T12:29:30.000+00:00"),
$lte: ISODate("2021-07-15T12:29:30.000+00:00")
}
}
}, {
$group: {
_id: {
$dateToString: {
format: "%Y-%m-%d-%H",
date: "$createdAt"
}
},
items: {
$first: '$$ROOT'
}
}
},{"$replaceRoot":{"newRoot":"$items"}}
,{"$sort":{"createdAt":-1}}
var 结果 = await _mongoDbContext.model.Aggregate(pipeline).ToListAsync();
【问题讨论】:
标签: c# mongodb asp.net-core aggregation-framework mongodb-.net-driver