Insert, Update, Delete REST API.

You can use your cloud search instance as a regular database where you insert, update and delete objects. This document provides examples of such operations/

Cloud search supports the mongoDB extended JSON encoding in both strict and shell mode. This allows you to properly encode dates and binary types.

Creating classes

This API call is only required if you want the class to be capped. Classes are usually created from the dashboard using the query pad.

curl -X POST \ -H "X-Nodechef-Application-Id: ${APPLICATION_ID}" \ -H "X-Nodechef-Master-Key: ${REST_API_KEY}" \ -H "Content-Type: application/json" \ -d '{ capped : 20000 }' \ https://cs.api.nodechef.com/sample/create # The capped attribute can be used to the limit the total number of objects the class can store.


Inserting documents

You can either specify the _id of the object or the search engine will auto create a string _id attribute for the object.

The insert request returns a JSON object with the number of documents inserted and also an attribute with name "_id" which contains a single element which the the Id of the object inserted into the class. This is useful for applications that need to retrieve the Id of a newly inserted object without having to generate its own Id in the application.

The Ids generated for each object is a string with exactly 10 characters.

curl -X POST \ -H "X-Nodechef-Application-Id: ${APPLICATION_ID}" \ -H "X-Nodechef-Master-Key: ${REST_API_KEY}" \ -H "Content-Type: application/json" \ -d '{"title": "Hello world" }' \ https://cs.api.nodechef.com/sample/insert


Updating objects

You must specify the query and the modifier attributes in the request. The format of the query and the modifier is compatible with mongodb query and update operators. Multiple documents matching the query will be updated. If the upsert value is true and no matching documents are found, a new documented will be inserted based on the modifier and the query specified. For a review of the upsert behaviour, please check the mongoDB docs.

curl -X POST \ -H "X-Nodechef-Application-Id: ${APPLICATION_ID}" \ -H "X-Nodechef-Master-Key: ${REST_API_KEY}" \ -H "Content-Type: application/json" \ -d '{"query": { "_id": "xH7e89922n" } \ "modifier": { $set : { scores : 20 } }, upsert : false }' \ https://cs.api.nodechef.com/gamestats/update


Deleting objects

Delete one or more objects from the class matching the specified query.

curl -X POST \ -H "X-Nodechef-Application-Id: ${APPLICATION_ID}" \ -H "X-Nodechef-Master-Key: ${REST_API_KEY}" \ -H "Content-Type: application/json" \ -d '{"_id": "xH7e819y2n" }' \ https://cs.api.nodechef.com/sample/delete


Multistatement Transactions

Perform multiple inserts, deletes and updates in a single transaction. The transaction is only committed to the database if all operations can be committed without any errors. Cloud search offers read committed isolation

To use this feature, make a request to the endpoint "https://cs-api.nodechef.com/mutlistatementtransaction" setting both your masterkey and application Id.

An example of the request body of a transaction is given below.

Note, it is possible to retrieve the Id generated for an object and use it in another statement. In the below example, we retrieve the Id generated for the first insert on orders and use it for another insert in the orderItems class. To retrieve the Id, simply reference the $system._id. namespace, and specify the index of the object for which you want to retrieve the _Id. In the example below, there is only one object inserted in orders which happens to be the first Id generated in the $system._id namespace, hence its index is zero, to retrieve this Id, simply use $system._id.0. To retrieve the Id of the insert on orderItems, which will be the second Id generated in the transaction, use $system._id.1
{ statements : [ { insert : "orders", values : [ { customerId : 'xx998823s', total : 239.9 } ] }. { update : "books", values : [ { query : { _id : '22xx99232' }, modifier : { $inc : { instock : -1 } } } ] }, { insert : "orderItems" values : [ { orderId : "$system._id.0", itemId : "eeevvvva" } ] } ] }

Because each statement element accepts an array for the values attribute, you could use this feature to perform batch inserts which will be significantly faster than making a request for each object to insert.