gpt4 book ai didi

c# - MongoDB $first 不支持的错误 C# 驱动程序

转载 作者:IT老高 更新时间:2023-10-28 12:32:11 25 4
gpt4 key购买 nike

我在 C# 驱动程序中有一个聚合查询,如下所示:

var result = await _records.Aggregate()
.Match(record => record.Statuses.Any(status => status.Status == currentStatus))
.Unwind(record => record.Statuses)
.Sort(Builders<BsonDocument>.Sort.Descending("statuses.date"))
.Group(doc => doc["_id"], group => new { Id = group.Key, CurrentStatus = group.First()["statuses"] })
.Match(arg => arg.CurrentStatus["status"] == BsonValue.Create(currentStatus.ToString()))
.ToListAsync();

与以下 shell 语法匹配:

db.records.aggregate(
[
{
$match : { "statuses.status" : "Finished" }
},
{
$unwind: "$statuses"
},
{
$sort: { "statuses.date":-1 }
},
{
$group: {
_id: "$_id",
current_status: { $first: "$statuses" }
}
},
{
$match : { "current_status.status" : "Finished" }
}
])

我认为 C# 版本由于 CurrentStatus = group.First()["statuses"] 而无法正常工作,异常(exception)情况是:

System.NotSupportedException: get_Item of type MongoDB.Bson.BsonValue is an unsupported method in a $project or $group pipeline operator.

我对 First() 约定的方法是基于此:

MongoDB C# Driver First Expression Reference

对如何以司机喜欢的方式翻译有什么想法吗?

最佳答案

在玩了一个类似的例子之后,看起来你可以通过首先在你的组上调用 Select 来获取 statuses 字段然后调用First() 关于那个:

var result = await _records.Aggregate()
.Match(record => record.Statuses.Any(status => status.Status == currentStatus))
.Unwind(record => record.Statuses)
.Sort(Builders<BsonDocument>.Sort.Descending("statuses.date"))
.Group(doc => doc["_id"], group => new
{
Id = group.Key,
CurrentStatus = group.Select(x => x["statuses"]).First()
})
.Match(arg => arg.CurrentStatus["status"] == BsonValue.Create(currentStatus.ToString()))
.ToListAsync();

关于c# - MongoDB $first 不支持的错误 C# 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32503030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com