DEV Community

Cover image for 🚀 Array operations API in API Maker
API Maker®
API Maker®

Posted on

🚀 Array operations API in API Maker

Auto generated schema-less array operation API is used for Mongo database only.

In the 'operations' array you can define the 'operation', 'path' and respective fields.

  • 'operation' can be 'push', 'addToset', 'pull', 'pullAll', 'pop' and 'set'.

    • 'push' it will add new given data to array.
    • 'addToset' adds a value to an array. If the value is already present it does nothing to that array.
    • 'pull' removes element by given query.
    • 'pullAll' operator removes all instances from array.
    • 'pop' removes the first or last element of an array.
    • 'set' used to replace the value of a field.
  • 'path' is the array type column name of the database.

  • 'dataToPush' is used for push operation. Array/Object of data to push in array.

  • 'queryToRemove' is used remove document from array.

  • 'dataToPull' remove all items of selected array.

  • 'direction' can be -1 or 1. -1 will remove first and 1 remove last from the array.

  • 'position' is used in push. If 'dataToPush' is array we can use it with $each operator.

  • 'slice' accept number value. It must use with $each, used in push. positive = remove that much elements from front of array. negative = remove items from end of array.

  • 'sort' must use with $each, used in push.

  • 'dataToSet' object to be passed in 'set' operator.

  • 'arrayFilters' array of objects, to be used to filter array items and set values of 'set' operator.

  • 'upsert' accept Boolean value if its true it check for the value and if value not found in 'set' operation it will insert new value there.

URL

/api/gen/user-path/instance/database/table/array-operations
Enter fullscreen mode Exit fullscreen mode

PUSH operation

Request Payload

{
    "find": {
        "customer_id": 1
    },
    "operations": [
        {
            "operation": "push",
            "path": "ages",
            "dataToPush": [
                {
                    "age": 33,
                    "birth_year": 1989,
                    "country_id": 1
                }
            ]
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode
  • Push in all objects.

Request Payload

{
    "find": {},
    "operations": [
        {
            "operation": "push",
            "path": "ages",
            "dataToPush": [
                {
                    "age": 2,
                    "birth_year": 1989
                }
            ]
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Pull operation

Request Payload

{
    "find": {
        "customer_id": 1
    },
    "operations": [
        {
            "operation": "pull",
            "path": "ages",
            "queryToRemove": {
                "age": 2,
                "birth_year": 1989,
                "country_id": 1
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Pop operation

  • It will remove a single record in ascending direction.

Request Payload:

{
    "find": {
        "customer_id": 5
    },
    "operations": [
        {
            "operation": "pop",
            "path": "ages",
            "direction": 1
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode
  • It will remove a single record in descending direction.

Request Payload:

{
    "find": {
        "customer_id": 5
    },
    "operations": [
        {
            "operation": "pop",
            "path": "ages",
            "direction": -1
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

PullAll operation

Request Payload

{
    "find": {
        "customer_id": 1
    },
    "operations": [
        {
            "operation": "pullAll",
            "path": "ages",
            "dataToPull": [
                {
                    "age": 2,
                    "birth_year": 1989,
                    "country_id": 1
                }
            ]
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode
  • Pull from all objects.

Request Payload

{
    "find": {},
    "operations": [
        {
            "operation": "pullAll",
            "path": "ages",
            "dataToPull": [
                {
                    "age": 2,
                    "birth_year": 1989
                }
            ]
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

addToSet

Request Payload

{
    "find": {},
    "operations": [
        {
            "operation": "addToSet",
            "path": "ages",
            "dataToPush": {
                "age": 2,
                "birth_year": 1989
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

set operation

  • The 'set' operation will update the value to an array. If the value is already present it does nothing to that array.
  • If we provide 'upsert:true' it will add 'field:value' if any related key is not found.
  • 'arrayFilters' array is used to find the data object in the array.

Request Payload

{
    "find": {},
    "operations": [
        {
            "operation": "set",
            "upsert": true,
            "dataToSet": {
                "ages.$[item].age": 4566,
                "ages.$[item].birth_year": 1999,
                "ages.$[item].birth_date": 29
            },
            "arrayFilters": [
                {
                    "item.birth_year": 1999
                }
            ]
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

slice

  • 'slice' accepts numbers.
  • If it's zero it will update the array with an empty array.
  • If it's negative it will update the array fields to contain only the last given number of elements.
  • If it's positive the array update with only the first given number of elements.

Request Payload

{
    "find": {
        "_id": "63e0775abd0e063920533f7c"
    },
    "operations": [
        {
            "operation": "push",
            "path": "ages",
            "dataToPush": [
                {
                    "age": 66,
                    "birth_year": 1961,
                    "country_id": ""
                }
            ],
            "slice": 2
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

position

  • Give the index number as 'position' and the data will push at that index.

Request Payload

{
    "find": {
        "_id": "63e0775abd0e063920533f7c"
    },
    "operations": [
        {
            "operation": "push",
            "path": "ages",
            "dataToPush": [
                {
                    "age": 66,
                    "birth_year": 1961,
                    "country_id": ""
                }
            ],
            "position": 2
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

sort

  • To get the sorted response array data use 'sort'.
  • It supports only two values 1 and -1.
  • Here, 1 will sort data in ascending order and -1 will give response data in descending order.

Request Payload

{
    "find": {
        "_id": "63e0775abd0e063920533f7c"
    },
    "operations": [
        {
            "operation": "push",
            "path": "ages",
            "dataToPush": [
                {
                    "age": 66,
                    "birth_year": 1961,
                    "country_id": ""
                }
            ],
            "sort": {
                "age": 1
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Push - Pull both operation in single payload.

Request Payload

{
    "find": {},
    "operations": [
        {
            "operation": "push",
            "path": "ages",
            "dataToPush": {
                "age": 2,
                "birth_year": 1989
            }
        },
        {
            "operation": "pull",
            "path": "ages",
            "queryToRemove": {
                "age": 2,
                "birth_year": 1989
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

$and use in operation in single payload.

Request Payload

{
    "find": {
        "customer_id": 3
    },
    "operations": [
        {
            "operation": "push",
            "path": "ages",
            "dataToPush": {
                "age": 4,
                "birth_year": 3112
            }
        },
        {
            "operation": "pull",
            "path": "ages",
            "queryToRemove": {
                "$and": [
                    {
                        "age": {
                            "$gte": 3
                        }
                    },
                    {
                        "birth_year": 7889
                    }
                ]
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

$or use in operation in single payload.

Request Payload

{
    "find": {
        "customer_id": 2
    },
    "operations": [
        {
            "operation": "push",
            "path": "ages",
            "dataToPush": {
                "age": 4,
                "birth_year": 3112
            }
        },
        {
            "operation": "pull",
            "path": "ages",
            "queryToRemove": {
                "$or": [
                    {
                        "age": {
                            "$gte": 3
                        }
                    },
                    {
                        "birth_year": 7889
                    }
                ]
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

auto convert String into Number datatype in dataToPush

  • Here age, birth_year, and country_id fields are numbers in API Maker's schema, we provide value in a string.

Request Payload

{
    "find": {
        "customer_id": 1
    },
    "operations": [
        {
            "operation": "push",
            "path": "ages",
            "dataToPush": [
                {
                    "age": "5",
                    "birth_year": "1990",
                    "country_id": "2"
                }
            ]
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

auto convert object into a string.

  • "hobbies" field is an array of strings. Here we try to save an object in that. It will convert objects into strings and save them.

Request Payload

{
    "find": {
        "customer_id": 4
    },
    "operations": [
        {
            "operation": "push",
            "path": "hobbies",
            "dataToPush": [
                {
                    "age": 999,
                    "birth_year": 2500
                },
                4896,
                ["data_type"],
                true,
                45.55
            ]
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

'queryToRemove' support object, string, number

Request Payload

{
    "find": {
        "customer_id": 2
    },
    "operations": [
        {
            "operation": "pull",
            "path": "ages",
            "queryToRemove": {
                "birth_year": 1991
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

To get more information about API Maker APIs please refer API Maker documentation.

API Maker array operation API documentation.

API Maker array operation API video.

Websites
https://apimaker.dev

Register and experience API Maker
https://cloud.apimaker.dev

Follow on twitter
https://twitter.com/api_maker

Linked In
https://www.linkedin.com/company/api-maker

API Maker Youtube channel
https://www.youtube.com/@api_maker

Top comments (0)