Array operations API
Auto generated schema-less array operation API is used for Mongo database only.
- To perform any operation on mongo database API Maker provides auto generated array operation API. Here, you can check some examples.
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
PUSH operation
Request Payload
{
"find": {
"customer_id": 1
},
"operations": [
{
"operation": "push",
"path": "ages",
"dataToPush": [
{
"age": 33,
"birth_year": 1989,
"country_id": 1
}
]
}
]
}
- Push in all objects.
Request Payload
{
"find": {},
"operations": [
{
"operation": "push",
"path": "ages",
"dataToPush": [
{
"age": 2,
"birth_year": 1989
}
]
}
]
}
Pull operation
Request Payload
{
"find": {
"customer_id": 1
},
"operations": [
{
"operation": "pull",
"path": "ages",
"queryToRemove": {
"age": 2,
"birth_year": 1989,
"country_id": 1
}
}
]
}
Pop operation
- It will remove a single record in ascending direction.
Request Payload:
{
"find": {
"customer_id": 5
},
"operations": [
{
"operation": "pop",
"path": "ages",
"direction": 1
}
]
}
- It will remove a single record in descending direction.
Request Payload:
{
"find": {
"customer_id": 5
},
"operations": [
{
"operation": "pop",
"path": "ages",
"direction": -1
}
]
}
PullAll operation
Request Payload
{
"find": {
"customer_id": 1
},
"operations": [
{
"operation": "pullAll",
"path": "ages",
"dataToPull": [
{
"age": 2,
"birth_year": 1989,
"country_id": 1
}
]
}
]
}
- Pull from all objects.
Request Payload
{
"find": {},
"operations": [
{
"operation": "pullAll",
"path": "ages",
"dataToPull": [
{
"age": 2,
"birth_year": 1989
}
]
}
]
}
addToSet
Request Payload
{
"find": {},
"operations": [
{
"operation": "addToSet",
"path": "ages",
"dataToPush": {
"age": 2,
"birth_year": 1989
}
}
]
}
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
}
]
}
]
}
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
}
]
}
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
}
]
}
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
}
}
]
}
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
}
}
]
}
$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
}
]
}
}
]
}
$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
}
]
}
}
]
}
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"
}
]
}
]
}
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
]
}
]
}
'queryToRemove' support object, string, number
Request Payload
{
"find": {
"customer_id": 2
},
"operations": [
{
"operation": "pull",
"path": "ages",
"queryToRemove": {
"birth_year": 1991
}
}
]
}