How to join two table where one table contain list of ID of another table in MongoDB using C#

Viewed 246

My data mode is like bellow example. I want to join my order & product table and group product details by Order as like SQL join in MongoDB using C# , LINQ. enter image description here

1 Answers

Bellow code is working using Aggregate & Lookup

public class ProductItemModel
{

    public string Id { get; set; }
    public Product[] Items { get; set; }
}

var _orderCollection = _context.GetCollection<Order>("Orders");
var _productCollection = _context.GetCollection<Product>("Products");

var data = (_orderCollection.Aggregate()
           //.Match(p => p.Id == "10000")
           .Lookup<Order, Product, ProductItemModel>(
                _productCollection,
                x => x.Items,
                x => x.Id,
                x => x.Items)
            ).ToList(); 
Related