FireDAC.MongoDB.ListView Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample shows how to work with a sample MongoDB dataset in a Delphi application.

Location

You can find the ListView sample project at:

  • Start | Programs | Embarcadero RAD Studio Sydney | Samples and navigate to:
    • Object Pascal\Database\FireDAC\Samples\DBMS Specific\MongoDB\ListView
  • Subversion Repository:
    • You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

Description

This sample application illustrates how to connect to a MongoDB server, select specific documents from a sample collection "restaurants" using a select query, parse the selected document elements (JSON items), and then display the results using the TListView component.

Using this sample application, you can:

Preliminary Steps

Before using this sample project, review the following checklist:

  • A MongoDB server is running and accessible from your host.

Note: If the MongoDB server is not running on the machine where you want to run this sample application, or requires non-default connection parameters, such as a port other than 27017, configure the FDConnection1 component accordingly. For details, see Connect to MongoDB Database.

  • 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:
MongoDB Restaurants Demo.png

You can find the MongoDB Restaurants Demo sample project at:

  • Start | Programs | Embarcadero RAD Studio Sydney | Samples and navigate to:
    • Object Pascal\Database\FireDAC\Samples\DBMS Specific\MongoDB\Restaurants
    • CPP\Database\FireDAC\Samples\DBMS Specific\MongoDB\Restaurants
  • Subversion Repository:
    • You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

How to Use the Sample

To run this project and select required data records

  1. Navigate to one of the locations given in the Location section above and open:
    • Delphi: Mongo_DataSet.dproj project file.
  2. Press F9 or choose Run > Run.
  3. To display the elements of the selected documents, click one of the following buttons:
    • Read using Find: selects all Italian restaurants from Manhattan, and then finds the required elements in a document using the TJSONIterator.Find method.
    • Read using Next/Recurse/Return: selects all Italian restaurants from Manhattan, and then iterates through the document elements using the TJSONIterator.Next/Recurse/Return methods. This allows the application to analyze each element (a JSON item) and find the required items in the elements tree.
    • Read using Iterate: selects all restaurants from Brooklyn, and then iterates through the document elements using the TJSONIterator.Iterate method. For each element, the TJSONIterator.Iterate method calls a callback function.

MongoDB ListView.png

Files

File in Delphi File in C++ Contains

Mongo_ListView.dproj

--

The project itself.

fMain.fmx

--

The main form where the components are located.

fMain.pas

--

Implementation of the sample.

Implementation

  • The project uses a TFDConnection component to establish a connection to a sample database.
  • To select specific documents from a sample database, the project uses the TMongoCollection.Find method.
  • To parse elements of the selected documents, the project uses various methods of TJSONIterator.
  • A TListView component displays the retrieved and parsed data.

Uses

See Also