MongoDB (version 2)
The MongoDB connector lets you perform insert, delete, update, and read operations on a MongoDB database.
Supported versions
- MongoDB versions 3.6 to 6.1
- Mongo Atlas deployed on Google Cloud.
Before you begin
Before using the MongoDB connector, do the following tasks:
- In your Google Cloud project:
- Ensure that network connectivity is set up. For information about network patterns, see Network connectivity.
- Grant the roles/connectors.admin IAM role to the user configuring the connector.
- Grant the following IAM roles to the service account that you want to use for the connector:
roles/secretmanager.viewer
roles/secretmanager.secretAccessor
A service account is a special type of Google account intended to represent a non-human user that needs to authenticate and be authorized to access data in Google APIs. If you don't have a service account, you must create a service account. For more information, see Creating a service account.
- Enable the following services:
secretmanager.googleapis.com
(Secret Manager API)connectors.googleapis.com
(Connectors API)
To understand how to enable services, see Enabling services.
If these services or permissions have not been enabled for your project previously, you are prompted to enable them when configuring the connector.
Configure the connector
Configuring the connector requires you to create a connection to your data source (backend system). A connection is specific to a data source. It means that if you have many data sources, you must create a separate connection for each data source. To create a connection, do the following steps:
- In the Cloud console, go to the Integration Connectors > Connections page and then select or create a Google Cloud project.
- Click + Create new to open the Create Connection page.
- In the Location section, choose the location for the connection.
- Region: Select a location from the drop-down list.
For the list of all the supported regions, see Locations.
- Click Next.
- Region: Select a location from the drop-down list.
- In the Connection Details section, complete the following:
- Connector: Select MongoDB from the drop down list of available Connectors.
- Connector version: Select the version 2
2
from the drop down list of available versions. - In the Connection Name field, enter a name for the Connection instance.
Connection names must meet the following criteria:
- Connection names can use letters, numbers, or hyphens.
- Letters must be lower-case.
- Connection names must begin with a letter and end with a letter or number.
- Connection names cannot exceed 49 characters.
- Optionally, enter a Description for the connection instance.
- Optionally, enable Cloud logging,
and then select a log level. By default, the log level is set to
Error
. - Service Account: Select a service account that has the required roles.
- Optionally, configure the Connection node settings:
- Minimum number of nodes: Enter the minimum number of connection nodes.
- Maximum number of nodes: Enter the maximum number of connection nodes.
A node is a unit (or replica) of a connection that processes transactions. More nodes are required to process more transactions for a connection and conversely, fewer nodes are required to process fewer transactions. To understand how the nodes affect your connector pricing, see Pricing for connection nodes. If you don't enter any values, by default the minimum nodes are set to 2 (for better availability) and the maximum nodes are set to 50.
- Database: The name of the MongoDB database.
- Connection Timeout: Connection Timeout in secs.
- Use SSL: Specify if SSL is enabled or not.
- App Name: Custom App Name for Mongo.
- Max Staleness Seconds: Time in seconds how stale a secondary can be before the client stops using it for read operations.
- Write Concern(w Option): Option to request acknowledgment that the write operation has propagated to a specified number of instances with specific tags.
- Write Concern(wtimeoutMS Option): Specifies a time limit, in milliseconds, for the write concern.
- Write Concern(journal Option): Requests acknowledgement from MongoDB that the write operation has been written to the journal.
- Read Preference: Set this to a strategy for reading from a replica set. Accepted values are primary, primaryPreferred, secondary, secondaryPreferred, and nearest.
- Cluster Type: Specify the type of MongoDB cluster.
- Enable Encryption: Specify whether encryption is enabled.
- Encryption Type: Specify the type of encryption enabled. Currently
only
Local Encryption
type is supported. - Encryption Schema: Specify the encryption schema information, including
the fields and encryption algorithm, key alt name, query type and contention factor.
For example:
{ "database.collection": { "encryptMetadata": { "algorithm": "parentAlgo" }, "properties": { "passportId": {}, "medicalRecords": { "encryptMetadata": { "algorithm": "medicalRecordParentAlgo" }, "properties": { "medicalRecord1": {}, "medicalRecord2": { "encrypt": { "algorithm": "medicalRecord2Algo", "keyAltName": "medicalRecord2Altkey", "queryType": "equality", "contentionFactor": 3 } } } }, "insuranceProperties": { "properties": { "policyNumber": {}, "insuranceRecord1": { "encrypt": { "algorithm": "insuranceRecord1Algo", "keyAltName": "insuranceRecord1AltKey" } }, "insuranceRecord2": { "encrypt": { "algorithm": "insuranceRecord2Algo", "keyAltName": "insuranceRecord2AltKey", "contentionFactor": 5 } } } } } } }
- encryption_master_key: Secret Manager Secret containing the encryption master key.
- encryption_key_vault: Encryption key vault value.
- Auth Database: Specify the name of the MongoDB database for authentication.
- Optionally, click + ADD LABEL to add a label to the Connection in the form of a key/value pair.
- Click NEXT.
- In the Destinations section, enter details of the remote host (backend system) you want to connect to.
- Destination Type: You can specify the destination details as
a host address, which can be any of the following:
- Single Mongo SRV string. For example,
mongodb+srv://cluster2.2f.mongodb.net
- Individual replicas. For example,
dasdsf.2f.mongodb01.net
- IP address. For example,
10.23.123.12
The
mongodb://
prefix will be added to final URL after combining all the replicas in connection string ifmongodb+srv
isn't specified.- Host address: Specify the hostname or IP address of the destination.
If you want to establish a private connection to your backend, do the following:
- Create a PSC service attachment.
- Create an endpoint attachment and then enter the details of the endpoint attachment in the Host address field.
To enter additional destinations, click +ADD DESTINATION.
- Single Mongo SRV string. For example,
- Click NEXT.
- Destination Type: You can specify the destination details as
a host address, which can be any of the following:
-
In the Authentication section, enter the authentication details.
- Select an Authentication type and enter the relevant details.
The following authentication types are supported by the MongoDB connection:
- Username and password
- Click NEXT.
To understand how to configure these authentication types, see Configure authentication.
- Select an Authentication type and enter the relevant details.
- Review: Review your connection and authentication details.
- Click Create.
Configure authentication
Enter the details based on the authentication you want to use.
-
Username and password
- Username: Username for connector
- Password: Secret Manager Secret containing the user's password.
- Auth Scheme: The scheme used for authentication.
The following Auth Scheme are supported by the MongoDB connection:
- Plain
- SCRAM-SHA-1
- SCRAM-SHA-256
Entities, operations, and actions
All the Integration Connectors provide a layer of abstraction for the objects of the connected application. You can access an application's objects only through this abstraction. The abstraction is exposed to you as entities, operations, and actions.
- Entity: An entity can be thought of as an object, or a collection of properties, in the
connected application or service. The definition of an entity differs from a connector to a
connector. For example, in a database connector, tables are the entities, in a
file server connector, folders are the entities, and in a messaging system connector,
queues are the entities.
However, it is possible that a connector doesn't support or have any entities, in which case the
Entities
list will be empty. - Operation: An operation is the activity that you can perform on an entity. You can perform
any of the following operations on an entity:
Selecting an entity from the available list, generates a list of operations available for the entity. For a detailed description of the operations, see the Connectors task's entity operations. However, if a connector doesn't support any of the entity operations, such unsupported operations aren't listed in the
Operations
list. - Action: An action is a first class function that is made available to the integration
through the connector interface. An action lets you make changes to an entity or entities, and
vary from connector to connector. Normally, an action will have some input parameters, and an output
parameter. However, it is possible
that a connector doesn't support any action, in which case the
Actions
list will be empty.
System limitations
The MongoDB connector can process a maximum of 70 transactions per second, per node, and throttles any transactions beyond this limit. By default, Integration Connectors allocates 2 nodes (for better availability) for a connection.
For information on the limits applicable to Integration Connectors, see Limits.
Supported data types
The following are the supported data types for this connector:
- BIGINT
- BINARY
- BIT
- BOOLEAN
- CHAR
- DATE
- DECIMAL
- DOUBLE
- FLOAT
- INTEGER
- LONGN VARCHAR
- LONG VARCHAR
- NCHAR
- NUMERIC
- NVARCHAR
- REAL
- SMALL INT
- TIME
- TIMESTAMP
- TINY INT
- VARBINARY
- VARCHAR
Configuration for the MongoDB Atlas cluster
The authentication configuration for a MongoDB Atlas cluster differ slightly from the previously documented steps. To authenticate against an Atlas cluster, you must first get the Atlas connection string. To obtain the Atlas connection string, perform the following steps:
- In the Clusters view, click Connect for the cluster you want to connect to.
- Click Connect Your Application.
- Select either driver option to display a connection string.
To authenticate against an Atlas cluster, use the following configuration:
- Auth Database: Set the value to admin, as all MongoDB Atlas users are associated with the admin database.
- Use SSL: Set the value to true.
- Server: Set the value to mongodb+srv://MongoDB INSTANCE SERVER NAME without including the username and password.
Actions
This section lists all the actions supported by the MongoDB connection.
AggregatePipeline action
This action aggregates the pipeline changes in a collection.
Input parameters of the AggregatePipeline action
Parameter name | Data type | Required | Description |
---|---|---|---|
collection | String | Yes | The name of MongoDB collection. |
pipeline | String | Yes | Aggregation pipeline containing field stages as list of strings. |
page_size | Integer | No | The number of items to be included in each page of the result set. |
page_no | Integer | No | The index of the page to be retrieved from the result set. Index starts from 0. |
For example on how to configure the AggregatePipeline
action, see Examples.
CountDocuments action
This action counts all the documents that satisfy a given condition in a collection.
Input parameters of the CountDocuments action
Parameter name | Data type | Required | Description |
---|---|---|---|
collection | String | Yes | The name of MongoDB collection. |
filter | String | No | A filter condition expressed in MongoDB query format. This condition can contain encrypted fields if encryption schema is enabled for querytype equality. |
Output parameters of the CountDocuments action
This action returns the count of all the documents that satisfy a given condition.
For example on how to configure the CountDocuments
action, see Examples.
DeleteDocuments action
This action deletes documents in a collection.
Input parameters of the DeleteDocuments action
Parameter name | Data type | Required | Description |
---|---|---|---|
collection | String | Yes | The Name of MongoDB collection. |
filter | String | No | A filter condition expressed in MongoDB query format. This condition can contain encrypted fields if encryption schema is enabled for query-type equality. |
Output parameters of the DeleteDocuments action
The action returns the count of documents that were deleted.
For example on how to configure the DeleteDocuments
action, see Examples.
DistinctValues action
This action returns the distinct values of a field in a collection.
Input parameters of the DistinctValues action
Parameter name | Data type | Required | Description |
---|---|---|---|
collection | String | Yes | The name of MongoDB collection. |
field | String | Yes | Field name to query for distinct values. |
filter | String | No | A filter condition expressed in MongoDB query format. This condition can contain encrypted fields if encryption schema is enabled for querytype equality. |
For example on how to configure the DistinctValues
action, see Examples.
InsertDocument action
This action inserts a document in a collection.
The following tables describe the input and output parameters of the InsertDocument
action.
Input parameters of the InsertDocument action
Parameter name | Data type | Required | Description |
---|---|---|---|
collection | String | Yes | The Name of MongoDB collection. |
document | String | No | Document to be added to a MongoDB collection. |
Output parameters of the InsertDocument action
This action returns the ID of the inserted document.
For example on how to configure the InsertDocument
action, see Examples.
ListDocuments action
The following tables describe the input and output parameters of the ListDocuments action.
Input parameters of the ListDocuments action
Parameter name | Data type | Required | Description |
---|---|---|---|
collection | String | Yes | The name of MongoDB collection. |
filter | String | No | A filter condition expressed in MongoDB query format. This condition can contain encrypted fields if encryption schema is enabled for querytype equality. |
page_size | Integer | No | The number of items to be included in each page of the result set. |
page_no | Integer | No | The index of the page to be retrieved from the result set. Index starts from 0. |
projection | String | No | The set of fields or properties to be included in the query projection in the Mongo format. |
sort_by | String | No | The specification of the query result ordering using MongoDB's Binary JSON (BSON) format. |
Output parameters of the ListDocuments action
The action returns the list of documents satisfying the filter condition as per the page size. The default page size is 25 and maximum page size is 50000.
For example on how to configure the ListDocuments
action, see Examples.
UpdateDocuments action
The following tables describe the input and output parameters of the UpdateDocuments
action.
Input parameters of the UpdateDocuments action
Parameter name | Data type | Required | Description |
---|---|---|---|
collection | String | Yes | The Name of MongoDB collection. |
values | String | No | Values to be updated in the Mongo format. |
filter | String | No | A filter condition expressed in MongoDB query format. This condition can contain encrypted fields if encryption schema is enabled for query-type equality. |
upsert | Boolean | No | Operation is upsert or not. |
Output parameters of the UpdateDocuments action
The action returns the number of documents that were updated.
For example on how to configure the UpdateDocuments
action, see Examples.
UpdateDocumentsWithEncryption action
This action allows the user to update the encrypted values.The following tables describe
the input and output parameters of the UpdateDocumentsWithEncryption
action.
Input parameters of the UpdateDocumentsWithEncryption action
Parameter name | Data type | Required | Description |
---|---|---|---|
collection | String | Yes | The Name of MongoDB collection. |
document | String | No | Document containing values to be updated. Values can contain fields that will be encrypted using CSFLE. |
filter | String | No | A filter condition expressed in MongoDB query format. This condition can contain encrypted fields if encryption schema is enabled for querytype equality. |
upsert | Boolean | No | Specifies if the operation is upsert . |
Output parameters of the UpdateDocumentsWithEncryption action
The action returns the number of documents that were updated.
For example on how to configure the UpdateDocumentsWithEncryption
action, see Examples.
Examples
This section describes how to perform some of the entity operations and actions in this connector.
Example - Aggregate pipeline changes
- In the
Configure connector task
dialog, clickActions
. - Select the
AggregatePipeline
action, and then click Done. - In the Task Input section of the Connectors task, click
connectorInputPayload
and then enter a value similar to the following in theDefault Value
field:{ "collection": "restaurants", "pipeline": { "stages": [ "{$addFields: { \"new_menu_item\": \"tea\"}}" ] }, "page_size": 1, "page_no": 0 }
This example adds a new menu item to all the documents in the restaurant
collection and
returns the first page of the result.
Example - Count documents
- In the
Configure connector task
dialog, clickActions
. - Select the
CountDocuments
action, and then click Done. - In the Task Input section of the Connectors task, click
connectorInputPayload
and then enter a value similar to the following in theDefault Value
field:{ "collection":"restaurants", "filter": "{\"name\": \"CarvelIceCream1\"}" }
This example returns the count of documents that match the filter criteria. If the
action is successful, the Connectors
task's
connectorOutputPayload
response parameter will have a value similar
to the following:
{ "results": [ { "count": 5 } ] }
Example - Delete documents
- In the
Configure connector task
dialog, clickActions
. - Select the
DeleteDocuments
action, and then click Done. - In the Task Input section of the Connectors task, click
connectorInputPayload
and then enter the following in theDefault Value
field:{ "collection": "Insurancedocuments", "filter": "{\"insurance.provider\":\"ABC\"}"}
This example deletes all the documents that have the insurance provider as ABC
.
Example - Find distinct values in a collection
- In the
Configure connector task
dialog, clickActions
. - Select the
DistinctValues
action, and then click Done. - In the Task Input section of the Connectors task, click
connectorInputPayload
and then enter a value similar to the following in theDefault Value
field:{"collection":"restaurants","field":"contact"}
This example returns the distinct values in the restaurants
collection. If the
action is successful, the Connectors
task's
connectorOutputPayload
response parameter will have a value similar
to the following:
{ "results": [ { "DistinctValues": [ "3217902323", "8747479023", "3492619023" ] } ] }
Example - Insert a document
- In the
Configure connector task
dialog, clickActions
. - Select the
InsertDocument
action, and then click Done. - In the Task Input section of the Connectors task, click
connectorInputPayload
and then enter a value similar to the following in theDefault Value
field:{ { "collection": "Insurancedocuments", "document": "{\"fname\":\"firstName\", \"lname\":\"lastName\", \"passportId\":\"ABCD1234\", \"bloodType\":\"A\", \"medicalRecords\":[{\"item\":\"name1\"},{\"item\":\"name2\"}], \"insurance\":{\"policyNumber\":\"12345\",\"provider\":\"provider2\"}}" } }
This example inserts a document in the Insurancedocuments
collection.
The content of the document is specified in the document
field. If the
insert is successful, you will get the ID of inserted document in
the Connectors
task's
connectorOutputPayload
response parameter.
Example - List documents
- In the
Configure connector task
dialog, clickActions
. - Select the
ListDocuments
action, and then click Done. - In the Task Input section of the Connectors task, click
connectorInputPayload
and then enter a value similar to the following in theDefault Value
field:{ "collection": "Insurancedocuments", "filter": "{\"insurance.provider\":\"ABC\"}", "page_size": 10 }
This example returns documents in the Insurancedocuments
collection
where the insurance provider is ABC
. The result set's page size
is set to 10 and list of files will be
available to you in JSON format in the Connectors
task's
connectorOutputPayload
response parameter.
Example - Update documents
- In the
Configure connector task
dialog, clickActions
. - Select the
UpdateDocuments
action, and then click Done. - In the Task Input section of the Connectors task, click
connectorInputPayload
and then enter a value similar to the following in theDefault Value
field:{"collection":"Insurancedocuments", "values":" {$set: { lname:\"Smith\" }}", "filter":"{\"insurance.provider\":\"ABC\"}"}
This example updates the last name to Smith
in the documents that match
the filter criteria. If the update is successful, you will get the count of
the number of documents that were updated. The response will be available in
the Connectors
task's
connectorOutputPayload
response parameter.
Example - Update documents with encryption
- In the
Configure connector task
dialog, clickActions
. - Select the
UpdateDocumentsWithEncryption
action, and then click Done. - In the Task Input section of the Connectors task, click
connectorInputPayload
and then enter the following in theDefault Value
field:{ {"collection":"Insurancedocuments", "document":" { \"insurance.provider\":\"XYZ\" }", "filter":"{\"insurance.provider\":\"ABC\"}"}
This example updates the insurance provider to XYZ
in all the documents that have the insurance provider as ABC
.
Use terraform to create connections
You can use the Terraform resource to create a new connection.To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.
To view a sample terraform template for connection creation, see sample template.
When creating this connection by using Terraform, you must set the following variables in your Terraform configuration file:
Parameter name | Data type | Required | Description |
---|---|---|---|
database | STRING | True | The name of the MongoDB database. |
timeout | INTEGER | False | Connection timeout in secs. |
enable_ssl | BOOLEAN | False | This field sets whether SSL is enabled. |
app_name | STRING | False | Custom app name for MongoDB. |
max_staleness_seconds | INTEGER | False | Time in seconds how stale a secondary can be, before the client stops using it for read operations. |
write_concern | STRING | False | Option to request acknowledgment that the write operation has propagated to a specified number of instances with specified tags. |
write_concern_timeout | INTEGER | False | Specifies a time limit, in milliseconds, for the write concern. |
write_concern_journal | BOOLEAN | False | Requests acknowledgement from MongoDB that the write operation has been written to the journal. |
read_preference | ENUM | False | Read preferences for this connection. Supported values are: primary, primaryPreferred, secondary, secondaryPreferred, nearest |
cluster_type | ENUM | False | Specifies the type of MongoDB cluster. Supported values are: REPLICA_SET, STANDALONE, SHARDED, UNKNOWN |
enable_encryption | BOOLEAN | False | This field sets whether Encryption is enabled. |
encryption_type | ENUM | False | This field sets type of encryption enabled. Supported values are: local |
encryption_schema | STRING | False | Encryption schema information, including the fields and encryption algorithm used. |
encryption_master_key | SECRET | False | Secret Manager Secret containing the encryption master key. |
encryption_key_vault | STRING | False | Encryption Key Vault value. |
auth_database | STRING | True | The name of the MongoDB database for authentication. |
Use the MongoDB connection in an integration
After you create the connection, it becomes available in both Apigee Integration and Application Integration. You can use the connection in an integration through the Connectors task.
- To understand how to create and use the Connectors task in Apigee Integration, see Connectors task.
- To understand how to create and use the Connectors task in Application Integration, see Connectors task.
Get help from the Google Cloud community
You can post your questions and discuss this connector in the Google Cloud community at Cloud Forums.What's next
- Understand how to suspend and resume a connection.
- Understand how to monitor connector usage.
- Understand how to view connector logs.