mongodb - .NET 驱动程序与 LINQ : NotSupportedException:

以下查询有效:

return Database
    .GetCollection<MyEntity>()
    .AsQueryable()
    .Where(x => x.StartDate <= instance && x.EndDate >= instance)
    .GroupBy(x => x.Key.Guid)
    .Select(x => x.First().Id)
    .ToList();

但是,当添加 $in 条件时(见下文),会抛出以下异常:

An unhandled exception was thrown by the application. System.NotSupportedException: $project or $group does not support First({document}{_id})

return Database
    .GetCollection<MyEntity>()
    .AsQueryable()
    .Where(x => guidKeys.Contains(x.Key.Guid)) // $in condition
    .Where(x => x.StartDate <= instance && x.EndDate >= instance)
    .GroupBy(x => x.Key.Guid)
    .Select(x => x.First().Id)
    .ToList();

我知道驱动程序还不支持很多 LINQ,但我不明白引入加法匹配阶段(使用 $in)会如何导致分组阶段不兼容。

有人能解释为什么会这样吗?

我正在使用带有 .NET 驱动程序 2.2.2 的 MongoDB 3.2。

编辑:

MyEntity 看起来像这样:

[BsonIgnoreExtraElements]
public class MyEntity: BaseMongoDocument
{
    [BsonId]
    [BsonRepresentation(BsonType.Binary)]
    public Guid Id { get; set; }

    [BsonRequired]
    [BsonElement("startDate")]
    public DateTime StartDate { get; set; }

    [BsonRequired]
    [BsonElement("endDate")]
    public DateTime EndDate { get; set; }

    [BsonRequired]
    [BsonElement("key")]
    public SquidDocument Key { get; set; }

    [BsonConstructor]
    private MyEntity()
    { }
}

public class SquidDocument
{
    [BsonRequired]
    [BsonElement("guid")]
    public Guid Guid { get; private set; }

    [BsonRequired]
    [BsonElement("squid")]
    public string Squid { get; private set; }

    [BsonConstructor]
    private SquidDocument(Guid guid, string squid)
    {
        Guid = realSquid.Guid;
        Squid = realSquid.Value;
    }
}

最佳答案

您的问题很可能不在您的 mongo 查询中,而是在 [guidKeys] 的解析中。因为 linq 会延迟执行,而 mongo 驱动程序不支持 linq,所以无法解析 [guidKeys.Contains] 和 barfs。将您的 guidKeys 转换为列表或数组,它可能会起作用。

关于mongodb - .NET 驱动程序与 LINQ : NotSupportedException: $project or $group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36838213/

相关文章:

javascript - 不确定如何在 Express/MongoDB 应用程序中处理数据访问对象/

mongodb - 玩 2.3.5 和 ReactiveMongo : MongoError ['N

spring - 蒙戈 : repositories no longer works

mongodb - Mongo中的SocketException

mongodb - 为什么 MongoDB 在负载测试期间没有响应?

mongodb - 如何验证由 mongodump 操作产生的文件的完整性?

ruby-on-rails - Moped::Errors::OperationFailure 失败

mongodb - Mongoid 没有服务器可用匹配首选项

mongodb - 对于只有部分数据易失的非常大的数据集,MongoDB 如何叠加

php - 在 PHP 中重命名 Mongo 集合