c# - 展开 MongoDB C# 驱动程序“MongoDB.Entities”后删除值
问题描述
此操作后的结果中 Title 和 Id 为空。必须说我对 mongodb piplines 或函数没有经验。
var articlePagination = await DB
.Fluent<NewsSubscription>()
.Match(o => o.Username == "peter" && !o.IsMuted)
.Lookup<NewsSubscription, Article, NewsSubscriptionArticle>(
DB.Collection<Article>(),
x => x.Channel,
x => x.Channels,
cwc => cwc.Articles)
.Unwind<NewsSubscriptionArticle, Article>(x => x.Articles)
.PagedSearch<Article, ArticleModel>()
.Sort(x => x.Title, Order.Ascending)
.Project(x => new ArticleModel
{
Id = x.ID,
Title = x.Title,
})
.PageSize(10)
.PageNumber(1)
.ExecuteAsync();
//Unwind后所有值都为null
public class NewsSubscription : IEntity
{
public string GenerateNewID()
{
return Guid.NewGuid().ToString();
}
[BsonId] public string ID { get; set; }
public string Channel { get; set; }
public bool IsMuted { get; set; }
public string Username { get; set; }
}
public class Article : IEntity
{
public string GenerateNewID()
{
return Guid.NewGuid().ToString();
}
[BsonId] public string ID { get; set; }
[Preserve] public string Title { get; set; }
public List<string> Channels { get; set; }
}
public class NewsSubscriptionArticle
{
public List<Article> Articles { get; set; }
}
有人可以解释价值在哪里以及如何将它们放在正确的位置。
编辑
我确实设法产生了正确的反应,但它看起来像意大利面条。
var lookup = new BsonDocument("$lookup",
new BsonDocument
{
{"from", "Article"},
{"localField", "Channel"},
{"foreignField", "Channels"},
{"as", "fromItems"}
});
var replaceRoot = new BsonDocument("$replaceRoot",
new BsonDocument("newRoot",
new BsonDocument("$mergeObjects",
new BsonArray
{
new BsonDocument("$arrayElemAt",
new BsonArray
{
"$fromItems",
0
}),
"$$ROOT"
})));
var articlePagination = await DB
.Fluent<NewsSubscription>()
.Match(o => o.Username == "peter" && !o.IsMuted)
.AppendStage<Article>(lookup)
.AppendStage<Article>(replaceRoot)
.PagedSearch<Article, ArticleModel>()
.Sort(x => x.Title, Order.Ascending)
.Project(x => new ArticleModel
{
Id = x.ID,
Title = x.Title,
})
.PageSize(10)
.PageNumber(1)
.ExecuteAsync();
解决方案
以下流利的管道应该做到这一点:
var (Results, TotalCount, PageCount) = await DB
.Fluent<NewsSubscription>()
.Match(s => s.Username == "peter" && !s.IsMuted)
.Project(x => new NewsSubscription { Channel = x.Channel })
.Lookup<NewsSubscription, Article, NewsSubscriptionArticle>(
DB.Collection<Article>(),
s => s.Channel,
a => a.Channels,
sa => sa.Articles)
.Unwind(x => x.Articles)
.ReplaceWith<Article>("$" + nameof(NewsSubscriptionArticle.Articles))
.PagedSearch<Article, ArticleModel>()
.Project(x => new ArticleModel
{
ArticleID = x.ID,
Title = x.Title
})
.Sort(am => am.Title, Order.Ascending)
.PageNumber(1)
.PageSize(100)
.ExecuteAsync();
更新:
没有NewsSubscriptionArticle
var (Results, TotalCount, PageCount) = await DB
.Fluent<NewsSubscription>()
.Match(s => s.Username == "UserOne" && !s.IsMuted)
.Project(x => new NewsSubscription { Channel = x.Channel })
.Lookup<NewsSubscription, Article, BsonDocument>(
DB.Collection<Article>(),
s => s.Channel,
a => a.Channels,
sa => sa["Articles"])
.Unwind(x => x["Articles"])
.ReplaceWith<Article>("$Articles")
.PagedSearch<Article, ArticleModel>()
.Project(x => new ArticleModel
{
Id = x.ID,
Title = x.Title
})
.Sort(am => am.Title, Order.Ascending)
.PageNumber(1)
.PageSize(100)
.ExecuteAsync();
推荐阅读
- javascript - Django Channels 失败:WebSocket 握手期间出错:net::ERR_CONNECTION_RESET
- php - 无法使用 PHPMailer 和 CRON 作业发送电子邮件
- javascript - 如何获取对象数组中的值?
- android - React Native - 根据背景颜色更改文本颜色
- python - socketio 发出事件的问题-python 2.7 客户端到 nodejs 服务器
- javascript - 如何使用 PHP 数据库中的数据将按钮添加到基于 JavaScript 的表中?
- xamarin - 如何在 Xamarin.Forms Shell 中使用自定义依赖注入?
- ios - for in 循环保持快速运行 firebase 数据库
- llvm - 我在哪里可以找到 QEMU (TCG) 的/LLVM IR 操作语义?
- tailwind-css - 我怎样才能让这个盒子有相同的宽度?