Gia
Gia

Reputation: 15

Values is dropped after Unwind MongoDB C# driver "MongoDB.Entities"

The Title and Id is empty in the result after this operation. Must say that I have no experience with mongodb piplines or functions.

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

//After Unwind all values are 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; }
    }

Can some explain where do the value go and how to put them on the rigth place.

EDIT

I did manage to produce a correct response with but it looks like spaghetti.

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

Upvotes: 1

Views: 332

Answers (1)

Dĵ ΝιΓΞΗΛψΚ
Dĵ ΝιΓΞΗΛψΚ

Reputation: 5689

the following fluent pipeline should do it:

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

update: without 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();

Upvotes: 1

Related Questions