MongoDB
Last updated
Last updated
MongoDB Source Connector
The MongoDB Connector provides the ability to read and write data from and to MongoDB. This document describes how to set up the MongoDB connector to run data reads against MongoDB.
The following table lists the field data type mapping from MongoDB BSON type to Nexus data type.
ObjectId
STRING
String
STRING
Boolean
BOOLEAN
Binary
BINARY
Int32
INTEGER
Int64
BIGINT
Double
DOUBLE
Decimal128
DECIMAL
Date
Date
Timestamp
Timestamp
Object
ROW
Array
ARRAY
For specific types in MongoDB, we use Extended JSON format to map them to Nexus STRING type.
Symbol
{"_value": {"$symbol": "12"}}
RegularExpression
{"_value": {"$regularExpression": {"pattern": "^9$", "options": "i"}}}
JavaScript
{"_value": {"$code": "function() { return 10; }"}}
DbPointer
{"_value": {"$dbPointer": {"$ref": "db.coll", "$id": {"$oid": "63932a00da01604af329e33c"}}}}
Tips
1.When using the DECIMAL type in Nexus, be aware that the maximum range cannot exceed 34 digits, which means you should use decimal(34, 18).
uri
String
Yes
-
The MongoDB standard connection uri. eg. mongodb://user:password@hosts:27017/database?readPreference=secondary&slaveOk=true.
database
String
Yes
-
The name of MongoDB database to read or write.
collection
String
Yes
-
The name of MongoDB collection to read or write.
schema
String
Yes
-
MongoDB's BSON and Nexus data structure mapping.
match.query
String
No
-
In MongoDB, filters are used to filter documents for query operations.
match.projection
String
No
-
In MongoDB, Projection is used to control the fields contained in the query results.
partition.split-key
String
No
_id
The key of Mongodb fragmentation.
partition.split-size
Long
No
64 1024 1024
The size of Mongodb fragment.
cursor.no-timeout
Boolean
No
true
MongoDB server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use. Set this option to true to prevent that. However, if the application takes longer than 30 minutes to process the current batch of documents, the session is marked as expired and closed.
fetch.size
Int
No
2048
Set the number of documents obtained from the server for each batch. Setting the appropriate batch size can improve query performance and avoid the memory pressure caused by obtaining a large amount of data at one time.
max.time-min
Long
No
600
This parameter is a MongoDB query option that limits the maximum execution time for query operations. The value of maxTimeMin is in Minute. If the execution time of the query exceeds the specified time limit, MongoDB will terminate the operation and return an error.
flat.sync-string
Boolean
No
true
By utilizing flatSyncString, only one field attribute value can be set, and the field type must be a String. This operation will perform a string mapping on a single MongoDB data entry.
common-options
No
-
1.The parameter
match.query
is compatible with the historical old version parametermatchQuery
, and they are equivalent replacements.
The following example demonstrates how to create a data synchronization job that reads data from MongoDB and prints it on the local client:
Unauthenticated single node connection:
Replica set connection:
Authenticated replica set connection:
Multi-node replica set connection:
Sharded cluster connection:
Multiple mongos connections:
Note: The username and password in the URI must be URL-encoded before being concatenated into the connection string.
In data synchronization scenarios, the matchQuery approach needs to be used early to reduce the number of documents that need to be processed by subsequent operators, thus improving performance. Here is a simple example of a Nexus using match.query
The following are examples of MatchQuery query statements of various data types:
In MongoDB, Projection is used to control which fields are included in the query results. This can be accomplished by specifying which fields need to be returned and which fields do not. In the find() method, a projection object can be passed as a second argument. The key of the projection object indicates the fields to include or exclude, and a value of 1 indicates inclusion and 0 indicates exclusion. Here is a simple example, assuming we have a collection named users:
In data synchronization scenarios, projection needs to be used early to reduce the number of documents that need to be processed by subsequent operators, thus improving performance. Here is a simple example of a Nexus using projection:
To speed up reading data in parallel source task instances, Nexus provides a partitioned scan feature for MongoDB collections. The following partitioning strategies are provided. Users can control data sharding by setting the partition.split-key for sharding keys and partition.split-size for sharding size.
By utilizing flat.sync-string
, only one field attribute value can be set, and the field type must be a String. This operation will perform a string mapping on a single MongoDB data entry.
Use the data samples synchronized with modified parameters, such as the following:
Source plugin common parameters, please refer to for details
Please refer to how to write the syntax of match.query
: