sidebar_position | description |
---|---|
9 |
Connect Appsmith to a MongoDB database and create queries. |
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
This page gives information to connect Appsmith to a MongoDB database and to read and write data in your applications.
:::caution
If you are a self-hosted user, you must whitelist the IP address of the Appsmith deployment 18.223.74.85
and 3.131.104.27
on your database instance or VPC before connecting to a database. See How to whitelist IP addresses on MongoDB Atlas for more details.
:::
The following section is a reference guide that provides a complete description of all the parameters to connect to a MongoDB database.
Connect MongoDB using Connection String URI- Yes: Connect to MongoDB database using the Connection String URI format
- No: Connect to MongoDB database by configuring multiple parameter fields.
mongodb+srv://<USERNAME>:<PASSWORD>@<HOST>/<DATABASE_NAME>
mongodb+srv://mockdb-admin:****@mockdb.kce5o.mongodb.net/movies?retryWrites=true&w=majority&authSource=admin
The following section lists the parameters to connect MongoDB by configuring multiple parameter fields instead of the Connection String URI format.
Connect MongoDB using multiple parameter fields- Read Only: This mode permits only read-only operations by default.
- Read/Write: This mode permits both read-write operations by default.
- Direct Connection: Enables you to connect to a single MongoDB instance
- Replicate Set: Enables you to connect to a group of connected instances that store the same set of data. This configuration provides data redundancy and high data availability. To connect to a replica set deployment, you must specify each instance's hostname and port numbers.
27017
by default if you don't specify one.
admin
, but you can specify a different database by using the Default Database Name parameter.
admin
.
- SCRAM-SHA-1: Provides secure password storage through the use of a salted hash function. See SCRAM-SHA-1 for details.
- SCRAM-SHA-256: Uses the SHA-256 hashing algorithm for password storage. See SCRAM-SHA-256 for details.
- MONGODB-CR: Uses a challenge-response protocol to authenticate users based on a combination of a password and a random challenge string. See MONGODB-CR for details.
:::info You cannot specify MONGODB-CR as the authentication mechanism when connecting to MongoDB 4.0+ deployments. :::
- Default: Depends on Connection Type field. If using the Replica set option, this is
Enabled
. If using the Direct Connection, this isDisabled
. - Enabled: Initiates the connection with TLS/SSL. Rejects the connection if SSL is not available.
- Disabled: Initiates the connection without TLS/SSL. Disallows all administrative requests over HTTPS. It uses a plain unencrypted connection.
The following section is a reference guide that provides a complete description of all the read and write operation commands with their parameters to create MongoDB queries.
Create MongoDB queries:::info See Query and Write Operation Commands for the MongoDB database commands. :::
This command fetches documents from a collection. The following section lists all the fields available for the Find Documents command.
{`{ rating: { $gte: 4 }, cuisine: {{cuisineList.selectedOptionValue}} }`}
cusineList
.{`{ name: 1 } `}
name
field in ascending order.{`{ name: 1, rating: 1, address: 1 } `}
name
, rating
, and address
fields in the matching documents.{`{{tableItems.pageSize}} `}
tableItems
is the name of the Table widget where you display the results from the query. The query limits the results based on the table widget's pageSize property.{`{{tableItems.pageOffset}} `}
This command inserts one or more documents and returns a document containing the status of all inserts. The following section lists all the fields for the Insert Documents command.
{`[ { "_id": {{NewMovieForm.data.idInput}}, "name": {{NewMovieForm.data.nameInput}}, "rating": {{NewMovieForm.data.ratingSelect}} } ]`}
This command modifies the documents in a collection based on a specified set of filters. The following section lists all the fields available for the Update Documents command.
{`{ "_id": {{tableItems.selectedRow.id}} }`}
_id
field is equal to the value in the id
column of the row selected on the Table widget named tableItems
.{`{ $set: { "name": {{tableItems.updatedRow.name}}, "rating": {{tableItems.updatedRow.rating}} } }`}
name
and rating
fields with the values updated in the table cells using inline editing.- Single Document: Limits the update to one document that meets the query criteria.
- All Matching Documents: Updates the fields in all documents that meet the query criteria.
This command removes one or more documents from the collection based on specified filters. The following section lists all the fields available for the Delete Documents command.
{`{ "rating": {{selectRating.selectedOptionValue}} }`}
rating
field contains the same value as the one selected in the Select widget named selectRating
.- Single Document: Limits the update to one document that meets the query criteria.
- All Matching Documents: Updates the fields in all documents that meet the query criteria.
This command counts the number of documents in a collection that match a specified set of criteria. The following section lists all the fields available for the Count command.
{`{ "release_dt": { $gte: {{releaseDate.formattedDate}} }`}
movies
collection where the release date is greater than the date picked in the Datepicker widget releaseDate
.This command finds the unique or distinct values for a specified field across a single collection. The following section lists all the fields available for the Distinct command.
{`{ "rating": {{selectRating.selectedOptionValue}} }`}
rating
field contains the same value as the one selected in the Select widget named selectRating
.This command allows users to process data records and return computed results. The aggregation framework provides several operators to perform a variety of operations like filtering, grouping, sorting, projecting, and calculating. See Aggregation Pipeline Stages for information on the aggregation operators. The following section lists all the fields available for the Aggregate command.
{`{ [ { $project: { tags: 1 } }, { $unwind: "$tags" }, { $group: { _id: "$tags", count: { $sum : 1 } } } ] }`}
articles
collection to calculate the count of each distinct element in the tags
array that appears in the collection.This command allows you to write queries using the MongoDB database command syntax. See Raw Query Commands for more information.
If you're experiencing difficulties, you can refer to to the Datasource troubleshooting guide, or guides for errors like:
If you still have trouble, you can contact the support team using the chat widget at the bottom right of this page.