跳到主要内容

MongoDB

MongoDB is a popular NoSQL document-oriented database that is widely used for web applications and other data-driven software. MongoDB is designed to be flexible, scalable, and fast, and is particularly well-suited for handling large amounts of unstructured or semi-structured data.

In Illa, you can connect to a MongoDB database using the MongoDB Query Resource. This resource allows you to execute MongoDB queries directly from Illa, without having to write any code. You can use the query editor to write queries in MongoDB's native query language, which is based on JavaScript and uses a JSON-like syntax for working with documents.

Create MongoDB

There are two ways to create a resource in Illa after signing into your Illa account.

  • Create in Resources

Sign into your Illa account, select **Resources** on the top of the page, and click **Create New** button.

external_resource

Select MongoDB from the database list.

action_list

Connect to the database with the required parameters described in Connection Settings below.

Click **Test Connection** to see if we can successfully connect to the database. If yes, click Save Resources, else, double check the hostname, port, username, and password is correct.

After creating a resource, the ready MongoDB will display as shown.

mongo_ex_ready

  • Create in Builder

Sign into your Illa account, create a project in Illa Builder in the **App** page, and navigate to the Action List at the bottom of the page. Click **new**, then select MongoDB from the database list. Then, connect to the database with required parameters described in Connection Settings below.

Click **Test Connection** to see if we can successfully connect to the database. If yes, click Save Resources, else, double check the hostname, port, username, and password is correct.

Connection Settings

Here we need to provide information for connecting to MongoDB database.

mongo_config

PropertiesRequiredDescription
NamerequiredThe name for resource when creating actions in the ILLA.
Config Typeoptionala type of collection used to store configuration data for sharded clusters.
HostnamerequiredThe URL or IP address for your database
Connection formatrequiredthe syntax used to specify the connection string for connecting to a MongoDB database or cluster.
PortrequiredThe server host's port number that you should use to connect. If you don't specify a port, default port is '3306'.
DatabaserequiredThe name of the database
Usernamerequiredthe username you wish to use when logging in to the MongoDB server.
PasswordrequiredUse this password for authentication.
SSL optionsoptionaldecides how high a secure SSL TCP/IP connection is prioritized while negotiating with the server.

Create Actions

We have created a MongoDB resource, we can add the action by selecting MongoDB from action list and choosing the Create action button.

mongo_resource_list

Now we have added the MongoDB server as an action to our building page.

mongodb

Configure MongoDB

Overview

Method NameDescription
Action Typeaggregate, bulkwrite, count, deleteMany. deleteOne, distinct, find, findOne
Collectiongroup of related documents that are stored together in a database
Transformertransforming data into the style you like using JavaScript

Aggregate

process multiple documents and return computed results.

Input

PropertiesRequiredDescription
Aggregationoptionaldefines the operations to be performed on the data

For example, to get all the results whose size is medium=>group the results by type=>Calculate the sum of prices in each type for Aggregation:

[
{
"$match": { "size": "medium" }
},
{
"$group": { "_id": "$type", "totalQuantity": { "$sum": "$price" } }
}
]

Output

An array of the computed results.

mongo_agg_data

Use {{mongodb1.data[0].result}} to get all.

mongo_agg_code

Bulkwrite

perform multiple write operations (insert, update, and delete) in a single request to the server

Input

PropertiesRequiredDescription
Operationsoptionalan object that specifies the type of operation to perform and the data to be written

An example for Operations:

[
{ "insertOne": { "document": { "_id": 3, "type": "beef", "size": "medium", "price": 6 } } },
{ "insertOne": { "document": { "_id": 4, "type": "sausage", "size": "large", "price": 10 } } },
{ "updateOne": {
"filter": { "type": "cheese" },
"update": { "$set": { "price": 8 } }
}
}
]

Output

The updated message is showing below.

mongo_bulk_data

Since this method make changes but does not return anything. There is no output data to access.

Count

count the number of documents that match a given query in a collection

Input

PropertiesRequiredDescription
Queryoptionalspecifies the selection criteria for the count operation

For example, to count the number of item with type “beef”, we may put below code into Query:

{"type":"beef"}

Output

The number of matching documents.

mongo_count_data

Use {{mongodb1.data}} to get it.

mongo_count_code

deleteMany

delete multiple documents that match a given filter in a collection.

Input

PropertiesRequiredDescription
Filteroptionalspecifies the selection criteria for the delete operation, If not specified, all documents in this collection will be deleted.

For example, to delete apples from items. We may put code below to Filter.

 {"type":"apple"}

Output

The number of matching documents.

mongo_delete2_data

Use {{mongodb1.data[0].result.DeletedCount}} to get it. If no files are matched, the returned value will be 0.

mongo_delete2_code

deleteOne

delete one document that match a given filter in a collection.

Input

PropertiesRequiredDescription
Filteroptionalspecifies the selection criteria for the delete operation

For example, to delete an apple from items. We may put code below to Filter.

 {"type":"apple"}

Output

Delete result.

mongo_delete1_data

Use {{mongodb1.data[0].result.DeletedCount}} to get it. If no files are matched, the returned value will be 0 (Since we deleted all the apples in deleteMany, there is no apple left, thus return 0).

mongo_delete1_code

distinct

retrieve an array of unique values for a specified field in a collection

Input

PropertiesRequiredDescription
Queryoptionalspecifies the selection criteria for the distinct operation
Fieldrequiredspecifies the field to retrieve the distinct values from.

For example, we may have {"type":"orange"} for Query and _id for Field.

Output

an array of distinct values

mongo_dist_data

Use {{mongodb1.data[0].result.map(item =>({"result":item}))}} to get the array.

mongo_dist_code

find

retrieve documents from a collection that match a specified set of criteria

Input

PropertiesRequiredDescription
Queryoptionalspecifies the selection criteria for the find operation
Projectionoptionalspecifies which fields to include or exclude in the query results
Sort Byoptionalspecify the sorting order of the returned documents
Limitoptionallimit the number of documents that are returned, default no limit. A limit of 0 is equivalent to no limit.
Skipoptionalspecify the number of documents to skip, default to 0.

For example, to find the id, price, and type of all oranges sorted by their id.

For Query, {"type": "orange"}

For Projection, {"_id": 1, "type": 1, "price": 1} For Sort By, {"_id":1}

Note: replace “1” with “true” still do it!

Output

An array of objects of documents

mongo_find_data

Use {{mongodb1.data[0].result}} to get the array.

mongo_find_code

findOne

retrieve the first document from a collection that match a specified set of criteria

Input

PropertiesRequiredDescription
Queryoptionalspecifies the selection criteria for the find operation
Projectionoptionalspecifies which fields to include or exclude in the query results
Skipoptionalspecify the number of documents to skip, default to 0.

For example, to find the id, price, and type of all oranges sorted by their id.

For Query, {"type": "orange"}

For Projection, {"_id": 1, "type": 1, "price": 1}

Note: replace “1” with “true” still do it!

Output

An array of objects of documents

mongo_find1_data

Use {{[mongodb1.data[0].result]}} to get the array.

mongo_find1_code