你需要
Project
操作人员要仅排除特定字段,可以这样做:
var allBooks = await repository.Find(_ => true)
.Project<Book>(Builders<Book>.Projection.Exclude(c => c.Pages))
.ToListAsync();
仅包括特定字段:
var allBooks = await repository.Find(_ => true)
.Project<Book>(Builders<Book>.Projection.Include(c => c.Pages))
.ToListAsync();
如果需要包含\排除多个字段,请多次调用(
Include(x => x.Title).Include(x => x.Title)
等等)。
仅包含所需内容的替代方法:
var allBooks = await repository.Find(_ => true).Project(b => new Book {
AuthorName = b.AuthorName,
Id = b.Id,
PublishYear = b.PublishYear,
Title = b.Title
// specify what you need, don't specify what you do not
// like Pages
}).ToListAsync();