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.Method | Path | Description |
---|---|---|
POST | / |
Create a database. |
POST | /databaseName |
Create a collection.
|
POST | /databaseName/collectionName |
Create a document.
|
- 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.Method | Path | Description |
---|---|---|
PUT | /databaseName/collectionName?queryParameters |
Update a document.
|
- 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.Method | Path | Description |
---|---|---|
GET | / |
List databases |
GET | /databaseName |
List collections
|
GET | /databaseName/collectionName?queryParameters |
Query the collection.
|
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.
|
GET | /databaseName/$cmd?query={command_document} |
Run the Informix or
MongoDB JSON command.
|
- 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.
- Run an SQL function
- This example runs an SQL function that adds two values.
- Query with a native cursor
- Use the following format to return query results in a native cursor:
DELETE
The DELETE method maps to the MongoDB delete command.Method | Path | Description |
---|---|---|
DELETE | / |
Delete all databases. |
DELETE | /databaseName |
Delete a database.
|
DELETE | /databaseName/collectionName |
Delete a collection.
|
DELETE | /databaseName/collectionName?queryParameter |
Delete all documents that satisfy the query
from a collection.
|
- 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}