首页 > 解决方案 > 展开 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();

标签: c#mongodbmongodb-entities

解决方案


以下流利的管道应该做到这一点:

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();

推荐阅读