Python Sub-Document Operations Question

Test Data :
{
“Items”: [
18,
{
“itemno”: 9857,
“qty”: 4,
“seqno”: 1,
“total_price”: 61600,
“unit_price”: 15400
},
{
“itemno”: 3271,
“qty”: 3,
“seqno”: 5,
“total_price”: 51300,
“unit_price”: 17100
},
{
“itemno”: 1366,
“qty”: 8,
“seqno”: 6,
“total_price”: 296800,
“unit_price”: 37100
}
],
“custid”: “cus_25245”,
“order_date”: “2023-03-01”,
“orderno”: “1”,
“ship_date”: “2023-03-02”
}

example 1 : update all qty value data

{
“Items”: [
18,
{
“itemno”: 9857,
“qty”: 100, <<-- update
“seqno”: 1,
“total_price”: 61600,
“unit_price”: 15400
},
{
“itemno”: 3271,
“qty”: 100, <<-- update
“seqno”: 5,
“total_price”: 51300,
“unit_price”: 17100
},
{
“itemno”: 1366,
“qty”: 100, <<-- update
“seqno”: 6,
“total_price”: 296800,
“unit_price”: 37100
}
],
“custid”: “cus_25245”,
“order_date”: “2023-03-01”,
“orderno”: “1”,
“ship_date”: “2023-03-02”
}

example 2 : update only data with qty value of 4

{
“Items”: [
18,
{
“itemno”: 9857,
“qty”: 1000, <<-- update
“seqno”: 1,
“total_price”: 61600,
“unit_price”: 15400
},
{
“itemno”: 3271,
“qty”: 3,
“seqno”: 5,
“total_price”: 51300,
“unit_price”: 17100
},
{
“itemno”: 1366,
“qty”: 8,
“seqno”: 6,
“total_price”: 296800,
“unit_price”: 37100
},
],
“custid”: “cus_25245”,
“order_date”: “2023-03-01”,
“orderno”: “1”,
“ship_date”: “2023-03-02”
}

example 3 : insert Items value

{
“Items”: [
18,
{
“itemno”: 9857,
“qty”: 4,
“seqno”: 1,
“total_price”: 61600,
“unit_price”: 15400
},
{
“itemno”: 3271,
“qty”: 3,
“seqno”: 5,
“total_price”: 51300,
“unit_price”: 17100
},
{
“itemno”: 1366,
“qty”: 8,
“seqno”: 6,
“total_price”: 296800,
“unit_price”: 37100
},
{
“itemno”: 9999, <<–insert
“qty”: 9999, <<–insert
“seqno”: 9999, <<–insert
“total_price”: 9999, <<–insert
“unit_price”: 9999 <<–insert
}
],
“custid”: “cus_25245”,
“order_date”: “2023-03-01”,
“orderno”: “1”,
“ship_date”: “2023-03-02”
}

example 4 : delete Items value

{
“Items”: [
18,
{
“itemno”: 9857,
“qty”: 4,
“seqno”: 1,
“total_price”: 61600,
“unit_price”: 15400
},
{
“itemno”: 3271,
“qty”: 3,
“seqno”: 5,
“total_price”: 51300,
“unit_price”: 17100
},
{
“itemno”: 1366,
“qty”: 8,
“seqno”: 6,
“total_price”: 296800,
“unit_price”: 37100
},
{
“itemno”: 9999, <<–delete
“qty”: 9999, <<–delete
“seqno”: 9999, <<–delete
“total_price”: 9999, <<–delete
“unit_price”: 9999 <<–delete
}
],
“custid”: “cus_25245”,
“order_date”: “2023-03-01”,
“orderno”: “1”,
“ship_date”: “2023-03-02”
}

How to do it in the Sub-Document Operations method

I checked the site, but I don’t know.

See the section on arrays in the documentation you pointed to. Then show us what you tried.

  • Mike

Hey @bellpumpkin

Example 1 - need to do a full-doc operation for this. Unless you know how many items there are? If so, you could use array syntax to write each one (but only up to 16 items, as 16 is the sub-doc limit). Or, you could use SQL++ with USE KEYS.
Example 2 - also need to do full-doc. Sub-doc does not have conditional expressions. Or, could use SQL++ here too.
Example 3 - this one you can use sub-doc, using array_append.
Example 4 - this you can’t, as sub-doc does not have conditional expressions. However, you could refactor items to be an object keyed off itemno, which would then let you easily delete that key. Or, SQL++ or full-doc.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.