MongoDB
This page gives information to connect Appsmith to a MongoDB database and to read and write data in your applications.
Connect MongoDB
If you are a cloud 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.
Connection parameters
The following section is a reference guide that provides a complete description of all the parameters to connect to a MongoDB database.
Use Mongo Connection String URI
- Yes: Connect to MongoDB database using the Connection String URI format
- No: Connect to MongoDB database by configuring multiple parameter fields.
Connection String URI
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.
Connection Mode
- Read Only: This mode permits only read-only operations by default.
- Read/Write: This mode permits both read-write operations by default.
Connection Type
- 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.
Host Address
Port
27017
by default if you don't specify one. Default Database Name
admin
, but you can specify a different database by using the Default Database Name parameter. Database Name
admin
.Authentication Type
- 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.
You cannot specify MONGODB-CR as the authentication mechanism when connecting to MongoDB 4.0+ deployments.
Username
Password
SSL Mode
- 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.
Query MongoDB
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.
See Query and Write Operation Commands for the MongoDB database commands.
Find Documents
This command fetches documents from a collection. The following section lists all the fields available for the Find Documents command.
Collection
Query
{ rating: { $gte: 4 }, cuisine: {{cuisineList.selectedOptionValue}} }
cusineList
.Sort
{ name: 1 }
name
field in ascending order.Projection
{ name: 1, rating: 1, address: 1 }
name
, rating
, and address
fields in the matching documents.Limit
{{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.Skip
{{tableItems.pageOffset}}
Insert Documents
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.
Collection
Documents
[ { "_id": {{NewMovieForm.data.idInput}}, "name": {{NewMovieForm.data.nameInput}}, "rating": {{NewMovieForm.data.ratingSelect}} } ]
Update Documents
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.
Collection
Query
{ "_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
.Update
{ $set: { "name": {{tableItems.updatedRow.name}}, "rating": {{tableItems.updatedRow.rating}} } }
name
and rating
fields with the values updated in the table cells using inline editing.Limit
- 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.
Delete Documents
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.
Collection
Query
{ "rating": {{selectRating.selectedOptionValue}} }
rating
field contains the same value as the one selected in the Select widget named selectRating
.Limit
- 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.
Count
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.
Collection
Query
{ "release_dt": { $gte: {{releaseDate.formattedDate}} }
movies
collection where the release date is greater than the date picked in the Datepicker widget releaseDate
.Distinct
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.
Collection
Query
{ "rating": {{selectRating.selectedOptionValue}} }
rating
field contains the same value as the one selected in the Select widget named selectRating
.Key
Aggregate
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.
Collection
Array of Pipelines
{ [ { $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 collectionLimit
Raw
This command allows you to write queries using the MongoDB database command syntax. See Raw Query Commands for more information.
Troubleshooting
If you're experiencing difficulties, you can refer to the Datasource troubleshooting guide, or contact the support team using the chat widget at the bottom right of this page.