Save data to MongoDB

    We first deploy a cluster instance named Cluster0 on MongoDB Cloud as a replica set :

    In this example, the cluster instance contains a master node and two slave nodes:

    image-20211126153431253

    Before entering the next step, we also need to configure the user password and IP access whitelist on the and Network Access pages to ensure normal access.

    After completing the above work, we will create MongoDB resources and rules in EMQX Dashboard.

    First, open the EMQX Dashboard, enter the resource page of the rule engine, click the Create button in the upper left corner, and the Create Resource form will pop up. In the Resource Type drop-down box in the form, we can see three resource types of MongoDB Single node mode, MongoDB Replica Set mode and MongoDB Sharded mode, which correspond to the three deployment methods of MongoDB.

    Here we select the MongoDB Replica Set mode, and then complete the configuration of the relevant parameters according to the actual situation of the MongoDB Server.

    The following is a description of some parameters of MongoDB resources:

    • SRV Record. it determines whether to query SRV and TXT records to obtain the server list and authSource and replicaSet options.
    • MongoDB Server. It specifies the server list or the domain name with DNS SRV and TXT records added.
    • Database Name, MongoDB database name.
    • Pool Size, the connection process pool size, which can help obtain the best performance with reasonable configuration.
    • Username, Password, authentication credentials.
    • Auth Source. It specifies the database used for authorization, and the default is admin. If SRV records are enabled and a DNS TXT record containing the authSource option is added to your MongoDB server domain name, the authSource option in this record will be used first.
    • Read mode. It can be set to master or slave_ok. When set to master, it means that the latest data will be read from the master node for each query. If not specified, the default value of master will be used.
    • Replica Set. If your MongoDB is deployed in replica set mode, you need to specify the corresponding replica set name. However, if the SRV record is set to true, and your MongoDB server domain name has a DNS TXT record with the replicaSet option, you can ignore this configuration item.
    • Enable SSL, whether to enable TLS connection. When set to true, more TLS related configurations will appear, please configure as needed. Note: SSL must be enabled when connecting to MongoDB Cloud.

    Go to , select the “rule” tab on the menu to the left. Then type in the following SQL:

    image-20211126201826084

    According to the status of SRV Record, we can configure MongoDB resources in the following two ways:

    We can click the Connect button of the Cluster0 instance on the Databases page of MongoDB Cloud, and select one of the three connection methods. Then, we can see the connection string to be used by the current instance. The selected part of the cursor is the content of the MongoDB Server field that we need to configure to the MongoDB resource of the EMQX rule engine later.

    Now, we continue to complete the configuration of MongoDB resources. Here we mainly made the following changes:

    1. Set SRV Record to true, and then set MongoDB Server to the domain name we just obtained.

    2. Set Database Name to test, which is the default database of MongoDB Cloud. You can configure it as needed.

    3. Configure Username and Password. You need to configure them according to the actual situation.

    4. Set Enable SSL to true. This is the connection requirement of MongoDB Cloud. Please configure as needed when deploying in other ways.

    image-20211129112203066

    Finally, we click the OK button at the bottom of the Create Resource form to complete the creation. At this time, a new MongoDB resource instance is successfully created in EMQX:

    TIP

    From EMQX Enterprise 4.4.11 and 4.3.17, we can use placeholders in ${var} format for the collections.

    2). Payload template. Payload template is the keys and values you’d like to insert into mongodb when the action is triggered. In this example we’ll insert all the available fields we got from the rule SQL in JSON format, so we just leave the payload template as empty.

    WARING

    If we choose not to enable SRV Record, then in the replica set and sharded mode, we need to fill in all the node addresses of the MongoDB cluster in the MongoDB Server option. In the replica set mode, we must also specify the replica set name.

    In order to quickly obtain the configuration information, we can use the command to query DNS records:

    Then fill in the queried server list in the MongoDB Server option in the format of host[:port][,...hostN[:portN]], and configure Auth Source and Replica Set according to the queried TXT record content:

    image-20211129143723391

    Finally, we also click the OK button at the bottom of the Create Resource form to complete the creation.

    Back to the creating rule page, then click on “Create” button. The rule we created will be show in the rule list.

    After the resource is created, we need to create the corresponding rules. Click the Create button at the upper left corner of the rule page to enter the Create Rule page, and enter the following SQL:

    This SQL means that all messages that match the topic filter t/# will trigger this rule, such as , t/1/2, etc. The filtered data, such as msgid and topic, can be used to perform subsequent actions.

    Click the Add Action button. For Action Type, select data persistence and data to MongoDB. Then, select a resource we just created in the Use Resource drop-down list. Configure Collection on demand, here I configure it as demo. Message content template remains empty, which means that the data filtered by SQL is converted into Json data in the form of a Key-Value list and is written to MongoDB. Multiple response actions can be added to each rule. Here we only need one response action. Therefore, after adding the following actions, we can click the Create button at the bottom of the page to complete the creation of the rule.

    image-20211129151712310

    We directly use the MQTT client tool in Dashboard to publish a message. In this example, we change the message topic to t/1 to hit the rules we set. The Payload and QoS remain unchanged. Then, click publish.

    image-20211129160418285