NoSQL Storage Service

App42 backend platform provides an easy interface to save your device data in the form of JSON objects using our Storage API. Your JSON data is saved inside a given collection and database. Here collection is like a table and your JSON object represents a row inside that collection. Your App can have only a single db and as many collections inside it.

Not setup your Raspberry Pi yet? Click here to follow the step-by-step guide

Import Statement

#include "App42API.h" 

Initialize

In order to use various functions available in a specific service, a developer has to initialize with App42API by passing the apiKey and secretKey which will be provided after the app creation from AppHQ dashboard.

Required Parameters

apiKey - The Application key given when the application was created. secretKey - The secret key corresponding to the application key given when the application was created.
App42API::Initialize("API_KEY", "SECRET_KEY");  

Build Service

After initialization, developer needs to call the buildXXXService method on App42API to get the instance of the particular API that they wish to build. For example, to build an instance of StorageService, buildStorageService() method needs to be called.

Sample_Class *sampleClassInstance = new Sample_Class();  

Insert Json Document

Save the JSON document in the given database name and collection name.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc has to be saved. json - Target JSON document to be saved.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
string json = "{\"Room Temperature\":\"30°C\",\"Device Type\":\"Raspberry Pi\"}";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->InsertJsonDocument(dbName, collectionName, jsonDoc, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));
void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Insert Json using Object

Save the JSON document in the given database name and collection name.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc has to be saved. json - Target JSON document to be saved.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
App42Object *object = new App42Object();
object->setObject("name", "Nick");
object->setObject("age", 30);
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->InsertJsonDocument(dbName, collectionName, object, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Find All Collections

Find all collections stored in the given database.

Required Parameters

dbName - Unique handler for storage name.
const char* dbName = "<Your_DataBase_Name>";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->FindAllCollections(dbName, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));
void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());
            printf("\n RecordCount=%lf\n",it->recordCount);
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Find All Documents

Find all documents stored in the given database and collection.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc has to be saved.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->FindAllDocuments(dbName, collectionName,app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));
void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());
            printf("\n RecordCount=%lf\n",it->recordCount);
            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
            	printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Find All Documents Count

Find the count of all the documents stored in the given database and collection.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be searched.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->FindAllDocumentsCount(dbName, collectionName,app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        printf("\nTotalRecords=%d",storageResponse->getTotalRecords());
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Find All Documents Paging

Find all the documents stored in the given database and collection by paging.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be searched. max - Maximum number of records to be fetched. offset - From where the records are to be fetched.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
int max = 2;
int offset = 0;
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->FindAllDocuments(dbName, collectionName,max,offset,app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));
void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());
            printf("\n RecordCount=%lf\n",it->recordCount);
            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
            	printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Find Document by Id

Find a target document by giving a unique object ID.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be searched. docId - Unique Object Id handler.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";    
const char* docId = "docId";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->FindDocumentById(dbName, collectionName, docId, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));
void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Find Document by Key Value

Find all the documents stored in the given database and collection by using key value search parameter.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be searched. key - Key to be searched for the target JSON doc. value - Value to be searched for the target JSON doc.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>"; 
const char* key = "name";
const char* value = "Nick";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->FindDocumentByKeyValue(dbName, collectionName,key,value,app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));
void Sample_Class::onStorageRequestCompleted( void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    
    for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
    {
        printf("\n Data Base Name=%s",it->dbName.c_str());
        printf("\n Collection Name=%s\n",it->collectionName.c_str());
    }
    
    for(std::vector<JSONDocument>::iterator it = storageResponse->documents.begin(); it != storageResponse->documents.end(); ++it)
    {
        printf("\n DocId=%s",it->docId.c_str());
        printf("\n Json =%s\n",it->jsonDoc.c_str());
    }
}  

Find Document by Query

Find target documents using Custom Query.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be searched. query - Query Object containing custom query for searching docs.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>"; 
const char* key1 = "name";
const char* value1 = "Nick";
const char* key2 = "age";
const char* value2 = "30";
Query *query1 = QueryBuilder::BuildQuery(key1, value1, APP42_OP_EQUALS);
Query *query2 = QueryBuilder::BuildQuery(key2, value2, APP42_OP_GREATER_THAN_EQUALTO);
Query *query3 = QueryBuilder::CompoundOperator(query1, APP42_OP_OR, query2);
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->FindDocumentByQuery(dbName, collectionName,query3,app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Event=%s",iit->getEvent().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Find Document by Query Paging

Find target documents using Custom Query with paging.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be searched. query - Query Object containing custom query for searching docs. max - max result parameter. offset - offset result parameter.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>"; 
const char* key1 = "name";
const char* value1 = "Nick";
const char* key2 = "age";
const char* value2 = "30";

Query *query1 = QueryBuilder::BuildQuery(key1, value1, APP42_OP_EQUALS);
Query *query2 = QueryBuilder::BuildQuery(key2, value2, APP42_OP_GREATER_THAN_EQUALTO);
Query *query3 = QueryBuilder::CompoundOperator(query1, APP42_OP_OR, query2);
int max =2;
int offset = 0;
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->FindDocumentsByQueryWithPaging(dbName, collectionName,query3,max,offset,app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Event=%s",iit->getEvent().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Find Docs with Query Paging Order by

Find target documents using Custom Query with paging and orderby.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be searched. query - Query Object containing custom query for searching docs. max - max result parameter. offset - offset result parameter. key - Key to be searched for the target JSON doc. type - Sorts the document by its type either ASCENDING or DESCENDING.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>"; 
const char* key1 = "name";
const char* value1 = "Nick";
const char* key2 = "age";
const char* value2 = "30";
Query *query1 = QueryBuilder::BuildQuery(key1, value1, APP42_OP_EQUALS);
Query *query2 = QueryBuilder::BuildQuery(key2, value2, APP42_OP_GREATER_THAN_EQUALTO);
Query *query3 = QueryBuilder::CompoundOperator(query1, APP42_OP_OR, query2);
int max =2;
int offset = 0;
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->FindDocsWithQueryPagingOrderBy(dbName, collectionName,query3,max,offset,key1,APP42_ORDER_ASCENDING, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Update Document by Key Value with JSON String

Update target document using a key value search parameter. This key value pair will be searched in the JSON doc stored in the cloud and matching doc will be updated with the new value passed. For example, you have an existing JSON document like: “{"name":"Nick Carter","Company":"Shephertz Technologies"}” and you need to update this json document with name ‘John Carter’ then in this case you need to pass the JSON document with updated value like “{"name":"John Carter","Company":"Shephertz Technologies"}”.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be updated. key - Key to be searched for the target JSON doc. value - Value to be searched for the target JSON doc. jsonDoc - Complete json document with updated value.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>"; 
const char* key = "name";
const char* value = "Nick";
const char* jsonDoc = "{\"Company\":\"Shephertz Technologies\"}";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->UpdateDocumentByKeyValue(dbName, collectionName, key, value, jsonDoc, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Update Document by Key Value with JSON Object

Update target document using a key value search parameter. This key value pair will be searched in the JSON doc stored in the cloud and matching doc will be updated with the new value passed. For example, you have an existing JSON document like: “{"name":"Nick Carter","Company":"Shephertz Technologies"}” and you need to update this JSON document with name ‘John Carter’ then in this case you need to pass the JSON document with updated value like “{"name":"John Carter","Company":"Shephertz Technologies"}”.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be updated. key - Key to be updated for the target JSON doc. value - Value to be updated for the target JSON doc. jsonDoc - Json document with updated values.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
const char* key = "name";
const char* value = "Nick";
App42Object *object = new App42Object();
object->setObject("name", "John");
object->setObject("age", "20");
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->UpdateDocumentByKeyValue(dbName, collectionName, key, value, object, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Update Document by Doc Id with JSON String

Update target document using the document ID. For example, you have an existing JSON document like: “{"name":"Nick Carter","Company":"Shephertz Technologies"}” and you need to update this JSON document with name ‘John Carter’ then in this case you need to pass the JSON document with updated value like “{"name":"John Carter","Company":"Shephertz Technologies"}”.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be updated. docId - Id of the document to be updated for the target JSON doc. jsonDoc - Complete Json document with updated values.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
const char* docId = "docId";
const char* jsonDoc = "{\"Company\":\"Shephertz Technologies\"}";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->UpdateDocumentByDocId(dbName, collectionName, docId, jsonDoc, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Delete Document by Doc Id

Delete target document using Object ID from the given database and collection. The Object ID will be searched in the JSON doc stored in the cloud and matching doc will be deleted.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be deleted. docId - Unique Object Id handler.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
const char* docId = "docId";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->DeleteDocumentsById(dbName, collectionName,docId,app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Delete All Documents

Delete all documents from selected database and collectionName.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which the documents need to be searched.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->DeleteAllDocuments(dbName, collectionName,app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));
void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Delete Documents by Key Value

Delete target document using key and value from the given database and collection. The key value will be searched in the JSON doc stored in the cloud and matching value will be deleted.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be deleted. key - Key to be searched for target JSON document which has to be deleted. value - Value to be searched for target JSON document which has to be deleted.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
const char* key = "name";
const char* value = "Nick";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->DeleteDocumentsByKeyValue(dbName, collectionName,key,value,app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Update Document by Doc Id with Json Object

Update target document with JSON Object doc using the document ID. For example, you have an existing JSON document like: “{"name":"Nick Carter","Company":"Shephertz Technologies"}” and you need to update this JSON document with name ‘John Carter’ then in this case you need to pass the JSON document with updated value like “{"name":"John Carter","Company":"Shephertz Technologies"}”.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be updated. docId - Id of the document to be updated for the target JSON doc jsonDoc - Complete json document with updated values.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
const char* key = "name";
const char* value = "Nick";
App42Object *object = new App42Object();
object->setObject("name", "John");
object->setObject("age", "20");
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->UpdateDocumentByKeyValue(dbName, collectionName, key, value, object, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Save Or Update Document by Key Value with JSON String

Update target document using a key value search parameter. This key value pair will be searched in the JSON doc stored in the cloud and matching doc will be updated with the new value passed. For example, you have an existing JSON document like: “{"name":"Nick Carter","Company":"Shephertz Technologies"}” and you need to update this JSON document with name ‘John Carter’ then in this case you need to pass the JSON document with updated value like “{"name":"John Carter","Company":"Shephertz Technologies"}”.

Note:If no document is searched with this key value pair then new document is inserted.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be updated. key - Key to be searched for the target JSON doc. value - Value to be searched for the target JSON doc. jsonDoc - Complete Json document with updated values.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
const char* key = "name";
const char* value = "Nick";
const char* jsonDoc = "{\"Company\":\"Shephertz Technologies\"}";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->SaveOrUpdateDocumentByKeyValue(dbName, collectionName, key, value, jsonDoc, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Save Or Update Document by Key Value with JSON Object

Update target document using a key value search parameter. This key value pair will be searched in the JSON doc stored in the cloud and matching doc will be updated with the new value passed. For example, you have an existing JSON document like: “{"name":"Nick Carter","Company":"Shephertz Technologies"}” and you need to update this JSON document with name ‘John Carter’ then in this case you need to pass the JSON document with updated value like “{"name":"John Carter","Company":"Shephertz Technologies"}”.

Note:If no document is searched with this key value pair then new document is inserted.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be updated. key - Key to be searched for the target JSON doc. value - Value to be searched for the target JSON doc. jsonDoc - Complete Json document with updated values.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
const char* key = "name";
const char* value = "Nick";
App42Object *object = new App42Object();
object->setObject("name", "John");
object->setObject("age", "20");
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->SaveOrUpdateDocumentByKeyValue(dbName, collectionName, key, value, object, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Update Document by Query with JSON String

Update target documents using a Custom Query. This Custom Query will be searched in the JSON doc stored in the cloud and matching doc will be updated with the new value passed. For example, you have an existing JSON document like: “{"name":"Nick Carter","Company":"Shephertz Technologies"}” and you need to update this JSON document with name ‘John Carter’ then in this case you need to pass the JSON document with updated value like “{"name":"John Carter","Company":"Shephertz Technologies"}”.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be updated. query - Query Object containing custom query for searching docs. jsonDoc - Complete Json document with updated values.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
const char* key1 = "name";
const char* value1 = "Nick";
const char* key2 = "age";
const char* value2 = "30";
Query *query1 = QueryBuilder::BuildQuery(key1, value1, APP42_OP_EQUALS);
Query *query2 = QueryBuilder::BuildQuery(key2, value2, APP42_OP_GREATER_THAN_EQUALTO);
Query *query3 = QueryBuilder::CompoundOperator(query1, APP42_OP_OR, query2);
const char* jsonDoc = "{\"Company\":\"Shephertz Technologies\"}";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->UpdateDocumentByQuery(dbName, collectionName, query3, jsonDoc, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Update Document by Query with JSON Object

Update target documents using a Custom Query. This Custom Query will be searched in the JSON doc stored in the cloud and matching doc will be updated with the new value passed. For example, you have an existing JSON document like: “{"name":"Nick Carter","Company":"Shephertz Technologies"}” and you need to update this JSON document with name ‘John Carter’ then in this case you need to pass the JSON document with updated value like “{"name":"John Carter","Company":"Shephertz Technologies"}”.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be updated. query - Query Object containing custom query for searching docs. jsonDoc - Complete Json document with updated values.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
const char* key1 = "name";
const char* value1 = "Nick";
const char* key2 = "age";
const char* value2 = "30";
Query *query1 = QueryBuilder::BuildQuery(key1, value1, APP42_OP_EQUALS);
Query *query2 = QueryBuilder::BuildQuery(key2, value2, APP42_OP_GREATER_THAN_EQUALTO);
Query *query3 = QueryBuilder::CompoundOperator(query1, APP42_OP_OR, query2);
App42Object *object = new App42Object();
object->setObject("name", "John");
object->setObject("age", "20");
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->UpdateDocumentByQuery(dbName, collectionName, query3, object, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Add Or Update Keys with JSON String

This function allows users to add or update the JSON document keys based on the object ID of JSON document. Using this method, you can add new keys or update the existing keys in current document. For example, you have an existing JSON document like: “{"name":"Nick Carter","Company":"Shephertz Technologies"}” and you need to update this JSON document with name John Carter. In this case you need to pass the JSON document with updated value like “{"name":"John Carter"}” and your new JSON document is: “{"name":"John Carter","Company":"Shephertz Technologies"}”.

Also, if you want to add a new key in existing document you can easily do so by using this method. For example, you have an existing JSON document like: “{"name":"Nick Carter","Company":"Shephertz Technologies"}” and you need to update this JSON document with name John Carter and first Name is John then in this case you need to pass the JSON document with value like “{"name":"John Carter","firstName":"John"}” and your new JSON document is: “{"name":"John Carter","firstName":"John","Company":"Shephertz Technologies"}”.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON keys needs to be updated. docId - Id of the document to be updated for the target JSON doc. keys - Json document with updated values.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
const char* docId = "docId";
const char* jsonDoc = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->AddOrUpdateKeys(dbName, collectionName, docId, jsonDoc, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Add Or Update Keys with JSON Object

This function will allow users to add or update the JSON document keys based on object ID of JSON document. Using this method, you can add new keys or update the existing keys in current document. For example, you have an existing JSON document like: “{"name":"Nick Carter","Company":"Shephertz Technologies"}” and you need to update this JSON document with name John Carter. In this case you need to pass the JSON document with updated value like “{"name":"John Carter"}” and your new JSON document is: “{"name":"John Carter","Company":"Shephertz Technologies"}”.

Also, if you want to add a new key in existing document you can easily do by using this method. For example, you have an existing JSON document like: “{"name":"Nick Carter","Company":"Shephertz Technologies"}” and you need to update this JSON document with name John Carter and first Name is John then in this case you need to pass the JSON document with value like “{"name":"John Carter","firstName":"John"}” and your new JSON document is: “{"name":"John Carter","firstName":"John","Company":"Shephertz Technologies"}”.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON keys needs to be updated. docId - Id of the document to be updated for the target JSON doc. keys - Json document with updated values.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
const char* docId = "docId";
App42Object *object = new App42Object();
object->setObject("name", "John");
object->setObject("age", "20");
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->AddOrUpdateKeys(dbName, collectionName, docId, object, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Sorting of JSON Documents

Find documents in sorted form according to given key stored in the database and collection by using key value search parameter. Note: Same can be done in all the find methods.

Required Parameters

dbName - Unique handler for storage name. collectionName - Name of collection under which JSON doc needs to be searched. key - Key to be searched for the target JSON doc. value - Value to be searched for the target JSON doc.
const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";
map<string,string>otherMetaHeaders;
otherMetaHeaders["orderByAscending"] = "<Name_Of_key_You_Want_To_Sort>";// Use orderByDescending for Descending or orderByAscending for Ascending as key
const char* key="name";
const char* value = "Nick";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->FindDocumentByKeyValue(dbName, collectionName,key,value,app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));

void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        for(std::vector<App42Storage>::iterator it = storageResponse->storages.begin(); it != storageResponse->storages.end(); ++it)
        {
            printf("\n DbName=%s",it->dbName.c_str());
            printf("\n CollectionName=%s",it->collectionName.c_str());            
            for(std::vector<JSONDocument>::iterator iit = it->jsonDocArray.begin(); iit != it->jsonDocArray.end(); ++iit)
            {
                printf("\n CreatedAt=%s",iit->getCreatedAt().c_str());
                printf("\n DocId=%s",iit->getDocId().c_str());
                printf("\n Owner=%s",iit->getOwner().c_str());
                printf("\n UpdatedAt=%s",iit->getUpdatedAt().c_str());
                printf("\n JsonDoc=%s\n",iit->getJsonDoc().c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}  

Exception Handling

The functions available under Storage API can throw some exceptions in abnormal conditions. For example, if an App developer is requesting for the document that does not exist, the function will throw the App42Exception (as shown below) with the message “Not Found” and the appErrorCode as “2600” and the details “Document by the Id ‘4faa3f1ac68df147a51f8bd7’ does not exist”.

const char* dbName = "<Your_DataBase_Name>";
const char* collectionName = "<Your_Collection_Name>";    
const char* docId = "docId";
App42API::Initialize("API_KEY", "SECRET_KEY");
StorageService *storageService = App42API::BuildStorageService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->FindDocumentById(dbName, collectionName, docId, app42callback(Sample_Class::onStorageRequestCompleted, sampleClassInstance));
void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
        //Handle Success Response here.
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
        
        int appErrorCode = storageResponse->appErrorCode;
        if(appErrorCode == 2600)
		{
			// Handle here for Not Found (Document by the Id '4faa3f1ac68df147a51f8bd7' does not exist.)
		}
		else if(appErrorCode  == 2606)
		{
			// Handle here for Bad Request (Document Id '4faa3f1ac68df147a' is not valid.)
		}
		else if(appErrorCode == 1401)
		{
			// handle here for Client is not authorized
		}
		else if(appErrorCode == 1500)
		{
			// handle here for Internal Server Error
		}
    }
}  

Error Codes

Functions in Storage API might throw exceptions with following HTTP and Application Error Codes (along with their descriptions):

1400 - BAD REQUEST - The Request parameters are invalid 1401 - UNAUTHORIZED - Client is not authorized. 1500 - INTERNAL SERVER ERROR - Internal Server Error. Please try again. 2600 - NOT FOUND - Document by the Id '@docId' does not exist. 2601 - NOT FOUND - Document by key '@key' and value '@value' does not exist. 2602 - NOT FOUND - No document in the collection '@collectionName' exists. 2603 - BAD REQUEST - The request parameters are invalid. Make Sure DB and collection name exists and functions are syntactically correct. 2604 - NOT FOUND - Storage with the name '@storageName' does not exist. 2605 - BAD REQUEST - Invalid Storage Query. 2606 - BAD REQUEST - Document Id '@docId' is not valid. 2608 - NOT FOUND - No document in the collection '@collectionName' exists for given query 2609 - NOT FOUND - The dataBase '@db' has no collections 2610 - BAD REQUEST - The request parameters are invalid. Storage '@storageName' already exists. 2611 - BAD REQUEST - The request parameters are invalid. Make Sure DB name Not contains special characters.