MongoDB

Convert your MongoDB database into RESTful API without any code.

Overview

MongoDB is a document-oriented NoSQL database used for high volume data storage. Instead of using tables and rows as in the traditional relational databases, MongoDB makes use of collections and documents. Documents consist of key-value pairs which are the basic unit of data in MongoDB.

Getting Started

After you create an account and log in to the dashboard, choose MongoDB service on the collections page. To make MongoDB API you need to provide your database credentials on the MongoDB creation form.

  • Connection String
  • Database
Attention needed
Make sure you already enable remote access to your MongoDB server and configured to accept connections from SlashAPI's IP Addresses.

API Endpoints

Table name act as API endpoints

The name of your table will be used as the API endpoint name. So, if database has a table called "users", the API URL will look like this:

https://v1.slashapi.com/<team>/mongodb/<identifier>/users

GET Records

To Get records from your MongoDB table, you just need to put your table name to the base API URL.

GET
<team>/mongodb/<identifier>/<table>

For example, if you want to get records from your users table, your API endpoint will look like this:

<team>/mongodb/<identifier>/users

In the GET records endpoint, you can use some of query parameters to allow you modify the fetched data. e.g sort by column, modify limit and pagination, etc.

Ordering

The reserved word is order_by. order_by is used to sort the fetched data in either ascending or descending according to one or more columns.

/users?order_by=name

By default the order_by sorts the data in ascending order. You can use the keyword desc after the column name to sort the data in descending order.

/users?order_by=name.desc

You can also sort the fetched data using multiple columns, Multiple order uses a comma separated.

/users?order_by=name.asc,id.desc

Limit and Pagination

SlashApi provides a page and per_page parameter that is used to paginate your data. SlashApi automatically takes care of setting the query's limit and offset based on the current page being viewed by the user. The current page is detected by the value of the page query string argument on the HTTP request.

To set the limit query, you need to set the per_page value in your query parameter to modify the number of items you would like to displayed. SlashApi set the default value for per_page option to 10. So, if you are not provide the per_page option in your API request, SlashApi will limit the number of items to 10.

Filtering

Database queries are created as valid JSON documents. A query object consists of fields and operators that make up a complete query.

Query example:

/users?q={"name":"John"}

Multiple parameters;

/users?q={"name":"John","age":17}
Filter Embedded Documents

To specify a query condition on fields in an embedded/nested document, use dot notation

/users?q={"field.nestedField":"John"}
Logic Operators
Abbr Description Example
$ne Not equal {"field":{"$ne":value}}
$like Like operator {"field":{"$like":value}}
$in Match any value in array {"field":{"$in":[value1,value2,...]}}
$nin Not match any value in array {"field":{"$nin":[value1,value2,...]}}
$or Logical operator {"$or":[{"field":"value"},{"field":"Another Value"}]}
$and Logical operator {"$and":[{"field":"value"},{"another_field":"value"}]}
Conditional Operators
Abbr Description Example
$gt Greater than {"field":{"$gt":value}}
$gte Greater than equal {"field":{"$gte":value}}
$lt Less than {"field":{"$lt":value}}
$lte Less than equal {"field":{"$lte":value}}
$between Matches field value between two numeric values {"field":{"$between":[1,5]}}
Date operator

Querying based on dates are done using the our predefined operator combined with date strings.

Operator Description
$date Compare a column's value against a date
$day Compare a column's value against a specific day
$month Compare a column's value against a specific month
$year Compare a column's value against a specific year

Query Example:

// Query equal date
{"created_at":{"$date":"2021-07-01"}}

// Query between two dates
{"created_at":{"$gt":{"$date":"2021-07-01"},"$lt":{"$date":"2021-07-20"}}}

// Compare a column's value against a specific day:
{"created_at":{"$day":7}}

// Compare a column's value against a specific month:
{"created_at":{"$month":7}}

// Compare a column's value against a specific year:
{"created_at":{"$year":2021}}

Selecting column

You may not always want to select all columns from a database table. Using the select query parameter, you can specify a custom "select" clause for the query:

/users?select=id,first_name,last_name

Get Specific Record

To get specific record of your table, you can specify the primary key as the identifier.

GET
<team>/mongodb/<identifier>/<table>/<id>

For example, if you want to get the records for posts table with _id = 610ab5f1d482594c47689b0b, you can use the URL like this:

/posts/610ab5f1d482594c47689b0b

Create Record

SlashApi also provides an endpoint that may be used to insert records into your database table. This endpoint using the POST method and accepts an array of column names and values. Missing properties will be set to the default values when applicable.

POST
<team>/mongodb/<identifier>/posts

Request Body

{
    "data": {
        "title": "Some title",
        "content": "Long text",
        "embed": {
            "field":"value"
        }
    }
}

Update Record

To update a row in a table, use the PATCH verb and specify the primary key as the identifier in your API URL path.

PATCH
<team>/mongodb/<identifier>/<table>/<_id>

Request Body

{
    "data": {
        "title": "Updated title",
        "content": "Updated text",
        "embed": {
            "field":"value"
        }
    }
}

Delete Record

To Delete a row in a table, use the DELETE verb and specify the primary key as the identifier in your API URL path.

DELETE
<team>/mongodb/<identifier>/<table>/<_id>