Connect to MongoDB Database (FireDAC)

From RAD Studio
Jump to: navigation, search

Go Up to Database Connectivity (FireDAC)

This topic describes how to connect to MongoDB using FireDAC.

MongoDB is an open-source NoSQL document database.

Obtaining MongoDB

You can download MongoDB from the official site at www.mongodb.org/downloads.

Supported Versions

The MongoDB FireDAC driver supports MongoDB version 3.0 and higher.

Client Software

Windows Client Software

FireDAC requires "MongoDB C driver" for connecting to MongoDB server. The RAD Studio installation includes the following precompiled libraries :

  • libbson-1.0.dll: a BSON library
  • libmongoc-1.0.dll: a MongoDB C driver.

The client libraries for connecting to MongoDB Server are included in the RAD Studio installation:

  • C:\Program Files (x86)\Embarcadero\Studio\23.0\Redist\Win32: Contains 32-bit versions of libbson-1.0.dll and libmongoc-1.0.dll
    • Use them for Win32 Desktop applications or for connection to MondoDB at design time.
  • C:\Program Files (x86)\Embarcadero\Studio\23.0\Redist\Win64: Contains 64-bit versions of libbson-1.0.dll and libmongoc-1.0.dll
    • Use them for Win64 Desktop applications.

Save the required libraries to any of the following locations:

  • a folder listed in your PATH environment variable (for example, <Windows>\SYSTEM32).
  • your application EXE folder.
  • any other folder, which you should specify in FDDrivers.ini:
[Mongo]
VendorLib=<folder>\libmongoc-1.0.dll

If the MongoDB client library has not been properly installed, an exception is raised when you try to connect:

[FireDAC][Phys][Mongo]-314. Cannot load vendor library [libmongoc-1.0.dll]. The specified module could not be found. Hint: check it is in the PATH or application EXE directories, and has x86 bitness.

macOS Client Software

FireDAC requires the following dynamic libraries for connecting to MongoDB Server:

  • libbson-1.0.0.dylib
  • libmongoc-1.0.0.dylib

These libraries are included in the RAD Studio installation, in the C:\Program Files (x86)\Embarcadero\Studio\23.0\Redist\osx32 folder.

Save them to /usr/local.

Linux Client Software

FireDAC requires the mongo-c-driver library. To install it:

  1. On Ubuntu Server 16.04 LTS, run:
    sudo apt-get install libmongoc
    sudo ln -s /usr/lib/x86_64-linux-gnu/libmongoc-1.0.so.0 /usr/lib/x86_64-linux-gnu/libmongoc-1.0.so
    sudo ln -s /usr/lib/x86_64-linux-gnu/libbson-1.0.so.0 /usr/lib/x86_64-linux-gnu/libbson-1.0.so
    
  2. On Red Hat Enterprise Linux 7, build mongo-c-driver from source, install it and run:
    sudo ln -s /usr/lib64/libmongoc-1.0.so.0 /usr/lib/x86_64-linux-gnu/libmongoc-1.0.so
    sudo ln -s /usr/lib64/libbson-1.0.so.0 /usr/lib/x86_64-linux-gnu/libbson-1.0.so
    

Driver Linkage

To link the driver:

Connection Definition Parameters

DriverID=Mongo

Parameter Description Example value
Server The TCP/IP address or host name of the server with the MongoDB database process running. 127.0.0.1
Port The TCP/IP port, on which the MongoDB database process is listening. 27017
Database Name of the current database for the connection. If the Database is not specified, no current database is set up. test
User_Name The MongoDB user ID.
Password The MongoDB user password.
UseSSL

Specify True to enable SSL connection. By default, it is set to False. Setting UseSSL=True requires you to additionally specify the connection definition parameters:

  • SSLPEMKeyFile -- is the path name to the .pem file that contains the SSL certificate and key.
  • SSLCAFile -- is the path name to the .pem file that contains the certificate from the Certificate Authority (CA).
  • SSLPEMKeyPassword -- if the client certificate-key file is encrypted.

For additional details, see the MongoDB documentation:

True
LoginTimeout Controls the amount of time, in seconds, before an application times out while attempting to establish a connection. 0
ReadTimeout The time-out in seconds for attempts to read from the server. Each attempt uses this time-out value and there are retrials if necessary, so the total effective time-out value is three times the option value. 300
MongoAdvanced Additional MongoDB database connection options:

For additional details, see the MongoDB documentation:

Use Cases

Using a Secure Connection

To use TLS/SSL encryption, define the UseSSL parameter with True as value, then specify the SSLPEMKeyFile and SSLCAFile parameters as follows:

DriverID=Mongo
Server=<ip_address>
Port=27017
Database=test
UseSSL=True
SSLCAFile=ca-cert.pem // The file that contains the certificate from the CA (Certificate Authority).
SSLPEMKeyFile=client-key.pem and client-cert.pem // The file that contains the SSL certificate and key.

You may also use the sslPEMKeyPassword if the client certificate-key file is encrypted. See the TLS/SSL Configuration for Client for more information.

Note: The MongoDB C driver has been compiled without SSL support. If your application requires SSL, you should compile the MongoDB C driver on your own and provide an additional set of required libraries. These libraries might require the OpenSSL and Microsoft Visual C++ Runtime libraries.

Other Usage Cases

  • Connect to a locally running server, listening on the default (27017) port:
DriverID=Mongo
Database=test
  • Connect to a remote server, listening on a non-default port with authentication enabled :
DriverID=Mongo
Server=test
Port=<port_number>
Database=test
User_Name=admin
Password=<admin_password>

See Also