Description

A Test Data Query performs a MongoDB query on the defined database resource. This MongoDB query can be performed in one of two ways for a database: Mongo Before and Mongo Each.


In This Article


When Should a MongoDB Query Be Used? 

  • Whenever you want to query real data from a MongoDB database and blend it with synthetically generated data. 
  • See this page for other types of databases: How to Add and Perform a Database Query.


2 Types of MongoDB Queries

  • Mongo Before - Query columns from one or more database collections as a set of data that is buffered into memory.
  • Mongo Each - Query one row of columns, from one or more database collections, on each row iteration of test data generation.

MongoDB Query Configuration Components

A Test Data Query configuration has the following primary components: 

  • Parameters - Define the resource path, sub-directory, name, and variables for the Test Data Query.
  • MongoDB Query Parameters - Defines the collection name, method name, query, sort, and projection, which constitute the MongoDB query to be performed on the database.
  • Domain Attributes - Defines the Attributes that will be populated by the real data obtained from the MongoDB query.

Below is a sample G-Query configuration for a MongoDB: 


Note: Only the available Parameters are different for these two types of MongoDB queries. All other setup steps are the same.


Prerequisite - MongoDB Properties File

A JDBC Config file (config.properties) must be available on your local computer to connect to the MongoDB database and perform the query. The properties file contains the database connection information and should be stored in the Resource Path location that is defined in the Query Parameters. 


Here are the parameters required in the properties file. By default, MongoDB runs on port number 27017 is the default. 
 

host=localhost
port=27017
database=anyDB
user=root
password=root


To obtain host and port information, run the following command: 


sudo lsof -iTCP -sTCP:LISTEN | grep mongo


Tip: You can create a separate properties file for each database to avoid the additional steps of changing the database information each time the database changes. They should be placed in a subdirectory, which should be included in the query parameters configuration.  


Kerberos Authentication Parameters

The following properties can be used to configure Kerberos Authentication. These properties need to be in the config.properties files and are an addition to the ones discussed in the previous section.

url=<mongoURL>
javax.security.auth.useSubjectCredsOnly=false
java.security.krb5.realm=<realm>
java.security.krb5.conf=<pathToKrb5Conf>
java.security.auth.login.config=<pathToGSSJaasConf>


How to Perform a MongoDB Query

The following provides the complete steps for setting up a MongoDB query and using it during test data generation.

Step 1 - Setup Your Project

  1. Select a Project Version.
  2. Create a Domain with Attributes. 
    • Note: This should include Attributes for the data values that will be queried from MongoDB.
  3. Set the Domain loopCount.
  4. Add a Receiver for the desired output format.
  5. Create a Scenario for the Domain.
  6. Configure Receiver Parameters and Property Keys.

 

Step 2 - Add a Query to the Project Version

  • Click on Add G-Query in the dashboard. 



  • Enter the Name and a Description
  • Select a Type (Query Before or Query Each) and click Save.



Step 3- Configure Query Parameters

Query Parameters define any resource variables to be used during the MongoDB Query. Available parameters vary based on the type of query.


For each query type, items with an asterisk (*) are required. 

  • Use the fields and drop-down menus to modify query parameters.
  • Click Save once finished.

 

Mongo Before


ParameterDescription
resourcePath*Defines the path where the resource file for MongoDB connection properties exists.
resourceSubDirDefines an optional sub-directory under the resourcePath where the resource file for MongoDB connection properties exists.
resourceName*Defines the name of the resource that contains the database connection information on a user's local machine.
bufferSize*Defines how many rows to keep in memory at a time. Note: Setting the number of rows to a very high number may cause an OutOfMemoryException to occur if the computer does not have enough available memory. The default buffer size is 1000 rows.
setLoop*setLoop provides the ability to OVERRIDE the loopCount, at the Scenario level, and to the number of rows that are queried. The default is set to False.
queryOnce*When the Domain that defines the query has a Parent Domain, it becomes a Child Domain of the Parent Domain. This flag determines if the Child Domain's query should be executed each time the Child Domain is called from the Parent Domain or if the query should only be executed once, the first time the Parent Domain calls the Child Domain. The default is set to 'True' to query once.
grVar1, grVar2, grVar3, grVar4, grVar5Five possible variables that can be used within the query.



Mongo Each


ParameterDescription
resourcePath*Defines the path where the resource file for MongoDB connection properties exists.
resourceSubDir
Defines an optional sub-directory under the resourcePath where the resource file for MongoDB connection properties exists.
resourceName*
Defines the name of the resource that contains the database connection information on a user's local machine. 
grVar1, grVar2, grVar3, grVar4, grVar5
Five possible variables that can be used within the query.


Step 4 - Enter the MongoDB Query Parameters

Mongo parameters vary based on whether the methodName is 'find' or 'aggregate'. Items with an asterisk (*) are required.


ParameterDescription
collectionName*
Defines the collection name for MongoDB Query.
methodName*
Defines the method name name for MongoDB Query. Method Name can be either find or aggregate.
query
Defines the MongoDB Query. This parameter is mandatory if methodName is 'aggregate'.
sort
Defines the sort parameter for MongoDB Query.
projection
Defines the projection parameter for MongoDB Query. This parameter is mandatory if methodName is 'find'.


Example MongoDB Parameters

The MongoDB Parameter form if methodName is find:




Step 5 - Add Domain Attributes

To finish the configuration, you will need to add the Domain Attributes. Data read from columns will be populated into the Domain Attributes for the Test Data Query. This will need to be done for each queried column of data in the database table.

  • Click on Add in the Domain Attributes pane.



  • Select the Domain and Attribute.
  • Enter the name of the Column being read by the query.
  • Enter a Default Value and click Save to finish.


  • Repeat these steps for each Domain Attribute.


  • Click on Column Check to ensure everything is correct.



Step 6 - (Optional) Add the Query to a Test Data Case

Queries can be added to one or more individual test cases. When this is done, the query does not have to be downloaded, just the case and Scenario. For this example, a case will not be used. To learn more, click here


Step 7 - Generate Test Data

It is time to generate the test data. The query will be performed during test data generation. 

  1. Download the Scenario, Scenario Chain, or Scenario Chain Set. 
  2. Download the Test Data Query.
  3. Copy the Query Command in the Dashboard.
  4. Open a Command Prompt or Terminal window.
  5. Run the command at the command line. 



Note: Click here to see step-by-step instructions. Please note that downloading is not necessary when using G-Repository.