Description

Using Mongo Query, the MongoEachToMapGen Generator will allow you to query one or more columns within a collection in a database allowing Find or Aggregate Query to be executed during EACH iteration of a given Generator. 


Note: Find or Aggregate Query at this level should only return one row in the result set. Even if it returns more than one row, the Generator will only retrieve the first row.

 

This Generator works in concert with the MongoEachFromMapGen Generator. The MongoEachFromMapGen Generator maps to a given column that is managed by a MongoEachToMapGen Generator to generate its data for a given Attribute.


Note: To learn more about the MongoEachFromMapGen Generator, please click here.

 

In This Article


Generator Parameters

The following parameters may be used to configure the MongoEachToMapGen Generator. Parameters with an asterisk (*) are required.

  • resourcePath* - Defines the path where the resource file for MongoDB connection properties exists.
  • resourceSubDir - Defines an optional subdirectory 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 - The first of five possible variables that may be used within the query.
  • grVar2 - The second of five possible variables that may be used within the query.
  • grVar3 - The third of five possible variables that may be used within the query.
  • grVar4 - The fourth of five possible variables that may be used within the query.
  • grVar5 - The fifth of five possible variables that may be used within the query.
  • collectionName* - Defines the collection name present in MongoDB Database.
  • methodName* - Defines the method name in MongoDB Query.
  • query - Defines the MongoDB Query to run.
  • sort  - Defines the sort in MongoDB Query.
  • projection - Defines the projection for MongoDB Query.
  • list - Stores values into a list that is used for preview data only within the GenRocket web platform.


MongoDB Query Example based on MongoEachToMapGen Parameters

The following shows how the Generator Parameters correspond to a MongoDB Query:


Sample Database Connectivity Properties file

A config.properties file is required to connect to a MongoDB database. An example file is shown below: 

host=localhost
port=27107
user=root
password=some123!
database=bank 
Java


URI Connection String Format for Connecting to any MongoDB Database

The MongoEachToMapGen Generator supports the URI connection string format to connect to a Mongo database.

To use the URI connection string, users need to configure their config.properties file to contain only the url property, which has the following format:


mongodb://[username:password@]host[:port][/[defaultauthdb][?options]]


Example

Here is an example config.properties file with the url property:

url=mongodb://user:password@localhost:27017/admin


Generic
Replace the user, password, host, and port information with your DB-specific details. In the above example, the Generator is going to validate the mentioned user against the admin database for the MongoDB running on localhost at port 27017.