I have Cosmos Document of below format:
(Note: Example is just for illustration, the actual document has different properties)
{
"OrderNumer":"12345",
"Version":"5",
"OrderDetails": [
{
"id":1,
"OrderItem": {
"Name": "ABC",
"Description": "ABC Description",
.
.
.
},
"PaymentDetails": {
},
.
.
.
},
{
"id":2,
"OrderItem": {
"Name": "PQR",
"Description": "PQR Description",
.
.
.
},
"PaymentDetails": {
},
.
.
.
},
],
"OrderDate": "12-01-2020",
"CustomerDetails": {
}
.
.
.
}
OrderNumber
is a partition key.
I'm trying to project the above document through Cosmos Query to get the entire OrderDetails
JSON object and a few properties from the parent into a single JSON with a filter on OrderDetails
-> id
.
The expected projection should be:
{
"Order":
[
{
"OrderNumer":"12345",
"Version":"5",
"id":1,
"OrderItem": {
"Name": "ABC",
"Description": "ABC Description",
.
.
.
},
"PaymentDetails": {
},
.
.
.
},
{
"OrderNumer":"12345",
"Version":"5",
"id":2,
"OrderItem": {
"Name": "PQR",
"Description": "PQR Description",
.
.
.
},
"PaymentDetails": {
},
.
.
.
},
]
}
As shown in the above JSON, I'm projecting OrderNumber
and Version
properties within the OrderDetails
as Order
.
OrderDetails
is a massive JSON with a dynamic schema that makes it difficult for us to project its properties individually.
I have tried a few options to project this, but the closest I could reach was below query:
SELECT c.OrderNumber, c.Version, o as Order
FROM c
JOIN o in c.OrderDetails WHERE
c.OrderNumber= '1235' AND ARRAY_CONTAINS([1,2], o.id)
However, the above query does not give desired result as it keeps the OrderNumber
and Version
in as separate property to Order
.
Is there any way I can achieve this?
SELECT c.OrderNumer,c.Version,o.OrderItem,o.PaymentDetails FROM c join o in c.OrderDetails
thennew JsonObject
add this result via code. – Steve ZhaoOrderDetails
andOrderDetails
has a huge and dynamic dataset. So I prefer to select it in entirety. Hope this clarifies – Ankit Vijay