MongoDB is a document database, which means it stores data in JSON-like documents, the most natural way to think about data, and is much more expressive and powerful than the traditional row/column model. How about connecting with MongoDB and exploring the available databases and data using FireDAC components in your Application. Don’t know where to start? This post will guide you to do that.
MongoDB.Explore Sample App shows how to connect to available MongoDB Databases, access to its ListCollections, and populate to the grid using MongoDB API wrapper class.
You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Components used in MangoDB.Explore Sample App:
- TFDPhysMongoDriverLink: To link the MongoDB driver to an application and set it up. In general, it is enough to only include the FireDAC.Phys.MongoDB unit into your application uses a clause. The TFDPhysMongoDriverLink component can be used to specify: The VendorHome – the MongoDB installation root folder. The VendorLib – the name and the optional path to the MongoDB client library.
- TFDConnection: To establish a connection to a DBMS and to manage associated datasets.
- TFDMongoDataset: Used to attach a dataset to an existing MongoDB cursor. This class allows you to attach the dataset to an existing MongoDB cursor. You can get the cursor from various MongoDB API wrapping class methods, such as TMongoDatabase.ListCollections.To open a dataset, assign a cursor to the Cursor property and call the Open method.
- TFDMongoQuery: The TFDMongoQuery class is used to execute a MongoDB query. You can specify a query in one of the following ways:
- Use TDataSource to populate the fetched data to TDBGrid.
Implementation Details:
You can find the MongoDB Explore Demo sample project at: Object PascalDatabaseFireDACSamplesDBMS SpecificMongoDBMongo_Explore
.dproj. Before running this, as a preliminary step, we need to have a MongoDB Server is running and accessible from your host. For Details, See Connect to MongoDB Database.
Check the below screen for listing the databases in the MongoDB server. Upon changing the database, its list collections changes. This screen Demonstrates the “restaurants” collection of the “test” database is provisioned with test data. To provision this collection, run the MongoDB Restaurants Demo, and click the Load Data button: You can find the MongoDB Restaurants Demo sample project at: Object PascalDatabaseFireDACSamplesDBMS SpecificMongoDBRestaurants
Check out the full article in the DocWiki about the MongoDB.Explore Sample.
Check out the full source code for the MongoDB.Explore projects for Delphi over on GitHub.
Check out the full source code for MondoDB samples in C++ over on GitHub.