FMX.FireDAC.MongoDB.DataSet Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample shows how to work with a sample MongoDB dataset in Delphi and C++Builder applications.

Location

You can find the DataSet sample project at:

  • Start | Programs | Embarcadero RAD Studio Rio | Samples and navigate to:
    • Object Pascal\Database\FireDAC\Samples\DBMS Specific\MongoDB\DataSet
    • CPP\Database\FireDAC\Samples\DBMS Specific\MongoDB\DataSet
  • 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.

Description

This sample application illustrates how to connect to a MongoDB server and select specific data from a sample collection "restaurants" using a select query. The select query helps you retrieve only the data that you want.

Using this sample application, you can:

  • Create a simple select query to retrieve the specific data records from the sample MongoDB collection "restaurants".
  • Browse the retrieved data and optionally make changes to the retrieved data records.

Preliminary Steps

Before using the DataSet 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 Rio | 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 above and open:
    • Delphi: Mongo_DataSet.dproj project file.
    • C++Builder: Mongo_DataSet.cbproj project file.
  2. Press F9 or choose Run > Run.
  3. In the JSON Query text box, specify a search query, and then click Fetch Restaurants to select specific data from the "restaurants" collection.

Search Query Examples

The search query is a MongoDB JSON query. For detailed information on the search query syntax, see MongoDB Manual.

To clarify, consider the following examples:

  • A simple query that is a comma-separated list of the field-value pairs inside curly braces (it corresponds to an SQL query: field1=value1 AND field2=value2 AND ....
    For example, to retrieve only restaurants with the Italian cuisine, which are located in Brooklyn, create the following select query: {"cuisine": "Italian", "borough": "Brooklyn"}

MongoDB DataSet Demo.png

  • The following query retrieves the restaurants with either Italian or Chines cuisine, which are situated in the 8 Avenue street (Brooklyn): {"cuisine": {$in: ["Chinese", "Italian"]}, "borough": "Brooklyn", "address.street": "8 Avenue"}

MongoDB DataSet Demo 2.png


To make changes to the "restaurants" collection

  1. Using the previous procedure, select the required data entries.
  2. In the restaurants list, make appropriate changes, and then click Save Restaurants to save these changes.

Files

File in Delphi File in C++ Contains

Mongo_DataSet.dproj

Mongo_DataSet.cbproj

The project itself.

fMain.fmx

fMain.fmx

The main form where the components are located.

fMain.pas

fmain.cpp

Implementation of the sample.

Implementation

  • The project uses a TFDConnection component to establish a connection with a sample database.
  • To select the data records from the database, the project uses a TFDMongoQuery, a TDataSource, and a TFDPhysMongoDriverLink.
  • A TDBGrid displays the retrieved data.
  • The FDMongoQuery1.CachedUpdates property is set to True to enable the dataset to log changes to the data without immediately applying them to the database. The application applies changes from the change log to the database after you click the Save Restaurants button.
  • The created JSON query is assigned to FDMongoQuery1.QMatch.
  • The "Coords" and "Grades" grid datasources are connected to the "address.coord" and "grades" nested datasets, respectively, at run time.

Uses

See Also