如何在C#Mongodb强类型驱动程序中基于嵌套数组元素属性总和进行过滤

时间:2018-12-17 07:58:22

标签: c# .net mongodb mongodb-query mongodb-.net-driver

我正在使用官方的C#MongoDb强类型驱动程序版本2.7.2与Windows 10计算机上的MongoDB v 4.0进行交互。

请考虑以下课程:

public class Library
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }

    public DateTime DateAdded { get; set; }

    public DateTime LastModified { get; set; }

    public string Title { get; set; }

    public Author Author { get; set; }

    public bool AllBooks { get; set; }
}

public class Author {
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string BirthDate { get; set; }

    public string ScientificDegree { get; set; }

    public List<Book> Books { get; set; }
}

public class Book
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }

    public string Title { get; set; }

    public int PublishYear { get; set; }

    public string Content { get; set; }

    public bool IsVerified { get; set; }

    public int DownloadCount {get; set; }
}

如何使用作者的总库至少下载1000次,这是我的代码:

string libraryId = GetLibraryId();

var repository = _database.GetCollection<Library>("Libraries");

var filter = Builders<Library>.Filter.Where(l => l.Id == libraryId &&
                l.Author.Books.Sum(b => b.DownloadCount) >= 1000);

var libraries = await repository.Find(filter).ToListAsync();

最后一行抛出System.ArgumentException:不支持的过滤器:Sum

0 个答案:

没有答案