我有一系列id的authorIds = [" 1"," 2",3"," 4"]。我有另一个数组,其中包含对authorIds的引用。 例如:
{{1}}
现在我想使用In查询(mongodb C#drvier),但希望将每个作者的书籍数量限制为5。有人能告诉我怎么做。
答案 0 :(得分:0)
您可以使用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;