REST API syntax

A subset of the HTTP methods is supported by the REST API. These methods are DELETE, GET, POST, and PUT.

The examples that are shown in this topic contain line breaks for page formatting; however, the REST API does not allow line breaks.

POST

The POST method maps to the MongoDB insert or create command.
Table 1. Supported POST method syntax
Method Path Description
POST / Create a database.
POST /databaseName Create a collection.
databaseName
The database name.
POST /databaseName/collectionName Create a document.
databaseName
The database name.
collectionName
The collection name.
Create a database
This example creates a database with the locale specified.
Request:
Specify the POST method:
POST / 
Data:
Specify database name mydb and an English UTF-8 locale:
{name:"mydb",locale:"en_us.utf8"}
Response:
The following response indicates that the operation was successful:
{"msg":"created db 'mydb'","ok":true}
Create a collection
This example creates a collection in the mydb database.
Request:
Specify the POST method and the database name as mydb:
POST /mydb
Data:
Specify the collection name as bar:
{name:"bar"}
Response:
The following response indicates that the operation was successful:
{"msg":"created collection mydb.bar","ok":true}
Create a relational table
This example creates a relational table in an existing database.
Request:
Specify the POST method and stores_mydb as the database:
POST /stores_mydb
Data:
Specify the table attributes:
{ name: "rel", 
  options: {
		columns:[{name:"id",type:"int",primaryKey:true,},
 			    {name:"name",type:"varchar(255)"},
	  		    {name:"age",type:"int",notNull:false}]
  }
}
Response:
The following response indicates that the operation was successful:
{msg: "created collection stores_mydb.rel" ok: true}
Insert a single document
This example inserts a document into an existing collection.
Request:
Specify the POST method, mydb database, and people collection:
POST /mydb/people
Data:
Specify John Doe age 31:
{firstName:"John",lastName:"Doe",age:31}
Response:
Here is a successful response:
{"n":1,"ok":true}
Insert multiple documents into a collection
This example inserts multiple documents into a collection.
Request:
Specify the POST method, mydb database, and people collection:
POST /mydb/people
Data:
Specify John Doe age 31 and Jane Doe age 31:
[{firstName:"John",lastName:"Doe",age:31},
{firstName:"Jane",lastName:"Doe",age:31}]	
Response:
Here is a successful response:
{"n":2,"ok":true}

PUT

The PUT method maps to the MongoDB update command.
Table 2. Supported PUT method syntax
Method Path Description
PUT /databaseName/collectionName?queryParameters Update a document.
databaseName
The database name.
collectionName
The collection name.
queryParameters
The supported Informix® queryParameters are query and options. The parameter named query maps to the equivalent MongoDB query. The options parameter can contain {"upsert":true/false} or {"multiUpdate":true/false}.
Update a document in a collection
This example updates the value for Larry in an existing collection, from age 49 - 25:
[{"_id":{"$oid":"536d20f1559a60e677d7ed1b"},"firstName":"Larry"
,"lastName":"Doe","age":49},{"_id":{"$oid":"536d20f1559a60e677d7ed1c"}
,"firstName":"Bob","lastName":"Doe","age":47}]
Request:
Specify the PUT method and query the name Larry:
PUT /mydb/people?query={firstName:"Larry"}
Data:
Specify the MongoDB $set operator with age 25:
{"$set":{age:25}}
Response:
Here is a successful response:
{"n":1,"ok":true}

GET

The GET method maps to the MongoDB query command.
Table 3. Supported GET method syntax
Method Path Description
GET / List databases
GET /databaseName List collections
databaseName
The database name.
GET /databaseName/collectionName?queryParameters Query the collection.
databaseName
The database name.
collectionName
The collection name.
queryParameters
The query parameters.
The supported Informix queryParameters are batchSize, query, fields, and sort. These map to the equivalent MongoDB batchSize, query, fields, and sort parameters.
GET /databaseName/$cmd?query={command_document} Run the Informix or MongoDB JSON command.
databaseName
The database name.
command_document
The Informix or MongoDB JSON command document. Specify the command document in the same format that is used by the db.runCommand() in the mongo shell.
GET /databaseName/system.sql?query={“$sql”:”sql_statement”} Run SQL passthrough query.
Note: You must enable SQL operations by setting security.sql.passthrough=true in the wire listener properties file.
databaseName
The database name.
sql_statement
Any SQL query or statement.
List databases
This example lists all of the databases on the server.
Request:
Specify the GET method and forward slash (/):
GET /
Data:
None.
Response:
Here is a successful response:
[ "mydb" , "test" ]
List all collections
This example lists all of the collections in a database.
Request:
Specify the GET method and mydb database:
GET /mydb
Data:
None.
Response:
Here is a successful response:
["bar"]
Query a collection and sort the results in ascending order
This example sorts the query results in ascending order by age.
Request:
Specify the GET method, mydb database, people collection, and query with the sort parameter. The sort parameter specifies ascending order (age:1), and filters id (_id:0) and last name (lastName:0) from the response:
GET /mydb/people?sort={age:1}&fields={_id:0,lastName:0}
Data:
None.
Response:
The first names are displayed in ascending order with the _id and lastName filtered from the response:
[{"firstName":"Sherry","age":31},
{"firstName":"John","age":31},
{"firstName":"Bob","age":47},
{"firstName":"Larry","age":49}]
Run the collStats command to get statistics about a collection
This example submits the MongoDB collStats command by using the REST API to get statistics about the jsonlog collection.
Here is the MongoDB shell syntax:
db.runCommand({collStats:"jsonlog"})
Request:
Specify the GET method, mydb database, and the collStats command document as the query:
GET /mydb/$cmd?query={collStats:"jsonlog"}
Data:
None.
Response:
[
	    {
	        "ns":"mydb.jsonlog",
	        "count":1000,
	        "size":322065,
	        "avgObjSize":322,
	        "storageSize":323584,
	        "numExtents":158,
	        "nindexes":1,
	        "lastExtentSize":2048,
	        "paddingFactor":0,
	        "flags":1,
	        "indexSizes":
	        {
	            "_id_":49152
	        },
	        "totalIndexSize":49152,
	        "ok":1
	    }
	]
Run an SQL function
This example runs an SQL function that adds two values.
Here is the MongoDB shell syntax:
> db.runCommand({runFunction:"add_values", "arguments":[3,6]}
Request:
Specify the GET method, mydb database, the runFunction parameter with the function name, and the arguments parameter with the argument values as the query:
GET mydb/$cmd?query={"runFunction":"add_values","arguments":[3,6]}
Data:
None
Response:
[{"returnValue": 9,"ok":1.0}]
Query with a native cursor
Use the following format to return query results in a native cursor:
GET /dbname/collectionname?query={query_condition}&nativeCursor=1
After you obtain the results, run the killCursors command to close the cursor.

DELETE

The DELETE method maps to the MongoDB delete command.
Table 4. Supported DELETE method syntax
Method Path Description
DELETE / Delete all databases.
DELETE /databaseName Delete a database.
databaseName
The database name.
DELETE /databaseName/collectionName Delete a collection.
databaseName
The database name.
collectionName
The collection name.
DELETE /databaseName/collectionName?queryParameter Delete all documents that satisfy the query from a collection.
databaseName
The database name.
collectionName
The collection name.
queryParameters
The query parameters.
The supported queryParameter is query. This map to the equivalent MongoDB query parameter.
Delete a database
This example deletes a database called mydb.
Request:
Specify the DELETE method and the mydb database:
DELETE /mydb
Data:
None.
Response:
Here is a successful response:
{msg: "dropped database", ns: "mydb", ok: true}
Delete a collection
This example deletes a collection from a database.
Request:
Specify the DELETE method, mydb database, and bar collection:
DELETE /mydb/bar
Data:
None.
Response:
Here is a successful response:
{"msg":"dropped collection", "ns":"mydb.bar", "ok":true}
Delete documents from a collection
This example deletes documents from a collection that contains the user "bob".
Request:
Specify the DELETE method, mydb database, people collection, and the query condition:
DELETE /mydb/people?query={user:"bob"}
Data:
None.
Response:
Here is a successful response where n indicates the number of documents deleted.
{"n":1,"ok":true}

Copyright© 2018 HCL Technologies Limited