Q:

c# mongodb find array of objects

i want to retrieve an array of objects in my document. In the shell i use this:
db.products.findOne({ProductId : 1}).Seller
In C# this is the structure of my classes
public class Product
{
    public double ProductId { get; set; }
    public string ProductName { get; set; }
    public string PurchaseDate { get; set; }
    public double Price { get; set; }
    public List<Seller> Seller { get; set; }
}

public class Seller
{
    public double SellerId { get; set; }
    public string SellerName { get; set; }
}

What is the equivalent command in mongodb.net driver?
db.products.findOne({ProductId : 1}).Seller
I expect the Output to be :
[
    {
        "SellerId" : 123,
        "SellerName" : "ABC"
    },
    {
        "SellerId" : 345,
        "SellerName" : "MGJ"
    }
]

Please see attached image output img

A:

I think you are looking for Projection. Here is how you do it:
public List<BsonDocument> GetSellers(double productId)
{
    var context = new Context();
    var filter = Builders<Product>.Filter.Eq(x => x.ProductId, productId);
    var project = Builders<Product>.Projection.Include(x => x.Sellers);
    var sellers = context.Product.Find(filter).Project(project).ToList();
    return sellers;
}

Note that a I changed Seller to Sellers since it is a list. I would also use ObjectId for the ids (instead of double or any other type).

