gpt4 book ai didi

c# - Mongodb In Query 对每条记录都有限制

转载 作者:可可西里 更新时间:2023-11-01 09:55:14 24 4
gpt4 key购买 nike

我有一个 id 的 authorIds=["1","2",3","4"] 数组。我有另一个数组保存对 authorId 的引用。例如:

book=[
{
bookId:"abcd",
authorId:"1"
},
{
bookId:"def",
authorId:"1"
}
{
bookId:"ghi",
authorId:"2"
}
{
bookId:"kjl",
authorId:"1"
}
];

现在我想使用 In 查询 (mongodb C# drvier),但想将每位作者的书籍数量限制为 5 本书。谁能告诉我该怎么做。

最佳答案

您可以使用Limit 选项来实现这一点:

var builder = Builders<Book>.Filter;
FilterDefinition<Book> filter = builder.In((b => b.authorId), authorIds);
var find = products.Find(filter);
find.Options.Limit = 5;

var bookList = find.ToListAsync().Result;

您还可以将 SortBy 组合到此:

var builder = Builders<Book>.Filter;
FilterDefinition<Book> filter = builder.In((b => b.authorId), authorIds);
var sorted = products.Find(filter).SortBy(b => b.bookId);
sorted.Options.Limit = 5;

var bookList = sorted.ToListAsync().Result;

关于c# - Mongodb In Query 对每条记录都有限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33629241/

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