Lillvik
Lillvik

Reputation: 405

Select children in Cosmos DB with parent included in the result

I have a product class that is stored in Cosmos DB together with its variants.

[DataContract]
public class Product
{
    [DataMember]
    public virtual string Name { get; set; }


    [DataMember]
    public virtual IList<Variant> Variants { get; set; }
}


[DataContract]
public class Variant
{
    [DataMember]
    public virtual string Name { get; set; }
}

I would like to query a projection of the variants that includes the Product.

[DataContract]
public class VariantProjection
{
    [DataMember]
    public virtual Product Product { get; set; }

    [DataMember]
    public virtual string Name { get; set; }
}

I'm using the DocumentDB Linq api, but if it's not possible with this api any other API would be ok.

Upvotes: 0

Views: 2198

Answers (1)

Matias Quaranta
Matias Quaranta

Reputation: 15583

Sounds like you are looking for JOINs and Projections (might want to try the Cosmos DB Query Playground, it has scenarios for both).

It would be great to have a simple dataset to test but I believe something like this might help:

SELECT p as product, variant.name  
FROM p
JOIN variant IN p.variants

Keep in mind though that you are retrieving the entire Product for each variant. That's what you are trying to achieve in your C# code.

Upvotes: 1

Related Questions