Subchapter 8.24
references/cms/cms-data-operations-extended.mdMarkdown6 KBView on GitHub
This recipe covers additional CMS data operations not included in the basic CRUD recipe.
Count items in a collection, optionally with filters.
Endpoint: POST /wix-data/v2/items/count
Count All Items:
curl -X POST \
'https://www.wixapis.com/wix-data/v2/items/count' \
-H 'Content-Type: application/json' \
-H 'Authorization: <AUTH>' \
-d '{
"dataCollectionId": "Products"
}'Response:
{
"totalCount": 42
}Count with Filter:
curl -X POST \
'https://www.wixapis.com/wix-data/v2/items/count' \
-H 'Content-Type: application/json' \
-H 'Authorization: <AUTH>' \
-d '{
"dataCollectionId": "Products",
"filter": {
"inStock": true
}
}'Count with Complex Filter:
{
"dataCollectionId": "Products",
"filter": {
"$and": [
{ "inStock": true },
{ "price": { "$gte": 50 } }
]
}
}Insert new items or update existing items in a single operation. This is useful for syncing data.
Endpoint: POST /wix-data/v2/bulk/items/save
Request Body:
{
"dataCollectionId": "Products",
"dataItems": [
{
"id": "existing-item-id",
"data": {
"title": "Updated Product",
"price": 199.99,
"inStock": true
}
},
{
"data": {
"title": "New Product",
"price": 79.99,
"inStock": true
}
}
],
"returnEntity": true
}| Scenario | Action |
|---|---|
No id provided | INSERT - Creates new item with generated ID |
id provided, doesn’t exist | INSERT - Creates new item with provided ID |
id provided, exists | UPDATE - Replaces existing item |
Warning: When updating, the entire item is replaced. Include all fields you want to keep.
Wix CMS doesn’t have a direct “update by filter” API. Use this two-step pattern:
curl -X POST \
'https://www.wixapis.com/wix-data/v2/items/query' \
-H 'Content-Type: application/json' \
-H 'Authorization: <AUTH>' \
-d '{
"dataCollectionId": "Products",
"query": {
"filter": {
"category": "electronics"
}
}
}'Important: Use
id(not_id) at the element level. Thedataobject should NOT contain_id.
curl -X POST \
'https://www.wixapis.com/wix-data/v2/bulk/items/update' \
-H 'Content-Type: application/json' \
-H 'Authorization: <AUTH>' \
-d '{
"dataCollectionId": "Products",
"dataItems": [
{
"id": "item-1-from-query",
"data": {
"title": "Updated Title 1",
"price": 99.99,
"onSale": true
}
},
{
"id": "item-2-from-query",
"data": {
"title": "Updated Title 2",
"price": 149.99,
"onSale": true
}
}
]
}'If you only want to update specific fields without replacing the entire item, use Bulk Patch:
Endpoint: POST /wix-data/v2/bulk/items/patch
Important: This endpoint uses
patchesarray withfieldModifications, NOTdataItems. Do not confuse with bulk update.
curl -X POST \
'https://www.wixapis.com/wix-data/v2/bulk/items/patch' \
-H 'Content-Type: application/json' \
-H 'Authorization: <AUTH>' \
-d '{
"dataCollectionId": "Products",
"patches": [
{
"dataItemId": "item-1",
"fieldModifications": [
{
"fieldPath": "onSale",
"action": "SET_FIELD",
"setFieldOptions": {
"value": true
}
}
]
}
]
}'Common error: If you get
WDE0080: patches must not be empty, you sentdataItemsinstead ofpatches. Use the format above.
Remove all items from a collection (dangerous operation).
Endpoint: POST /wix-data/v2/items/truncate
curl -X POST \
'https://www.wixapis.com/wix-data/v2/items/truncate' \
-H 'Content-Type: application/json' \
-H 'Authorization: <AUTH>' \
-d '{
"dataCollectionId": "TestCollection"
}'Warning: This permanently deletes ALL items in the collection. Use with extreme caution.
Perform calculations on collection data using a pipeline of sequential stages.
Endpoint: POST /wix-data/v2/items/aggregate-pipeline
Count by Category:
curl -X POST \
'https://www.wixapis.com/wix-data/v2/items/aggregate-pipeline' \
-H 'Content-Type: application/json' \
-H 'Authorization: <AUTH>' \
-d '{
"dataCollectionId": "Products",
"pipeline": {
"stages": [
{
"group": {
"groupIds": [
{"key": "category", "expression": {"fieldPath": "category"}}
],
"accumulators": [
{
"resultFieldName": "count",
"sum": {"expression": {"numeric": 1}}
}
]
}
}
]
}
}'| Operation | Use Case | Behavior |
|---|---|---|
| Bulk Insert | Add new items only | Fails if ID exists |
| Bulk Update | Update existing items | Fails if ID doesn’t exist, replaces entire item |
| Bulk Save | Upsert (insert or update) | Creates or updates based on ID |
| Bulk Patch | Partial update | Only modifies specified fields |