The requirements is "Find all the posts with the given tag if it exists. Otherwise, return all the posts". And I try the following
[HttpGet]
public async Task<ActionResult> Posts(string tag = null)
{
var blogContext = new BlogContext();
var posts = await blogContext.Posts.Find(Builders<Post>.Filter.AnyEq(x => x.Tags, tag) )
.Sort(Builders<Post>.Sort.Descending("CreatedAtUtc")).ToListAsync();
return View(posts);
}
My question is how to retrieve all documents when it is not match? thanks,
我知道了。
var posts = await blogContext.Posts.Find(x => (string.IsNullOrEmpty(tag) || x.Tags.Contains(tag))).Sort(Builders<Post>.Sort.Descending("CreatedAtUtc")).ToListAsync();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.