In this section:

Introduction

The MongoDB Tool sends a query to a MongoDB database and returns the JSON-format results from that query. JSON tools such as JSON Data Banks and JSON Assertors can be added as "outputs" to MongoDB tools in order to display and/or validate the query results.

Requirements

  • The following MongoDB versions are supported:
    • 2.6
    • 3.0
    • 3.2
    • 3.4
    • 3.6
    • 4.0
    • 4.2
    • 4.4
  • This tool requires SOAtest/Virtualize 2020.1 or later.

Installation

The tool can be installed from the UI or the command line.

UI Installation

  1. Go to Parasoft > Preferences and click System Properties.
  2. Click Add JARs and select the com.parasoft.soavirt.tool.mongodbtool.jar file.
  3. Click Apply.
  4. Restart SOAtest/Virtualize.

Command Line Installation

Add the com.parasoft.soavirt.tool.mongodbtool.jar file to the system.properties.classpath property in your settings properties file. For example:

system.properties.classpath=<PATH_TO_JAR>/com.parasoft.soavirt.tool.mongodbtool.jar

Usage

You can add MongoDB tools as standalone tools with the SOAtest Add Test wizard. You can also add them as Virtualize provisioning action tools. Before the tool can be run, you must configure the MongoDB connection, as well as the query you want to run. Once a MongoDB tool is added to a suite, you can add any JSON tool as an output.

Configuration

You can configure the following settings on the Tool Settings tab and the Input tab.

Tool Settings Tab

Connection Settings

Hosts

Comma-separated list specifying the host(s) and port(s) of the MongoDB server(s) to query. If empty, the default value will be used. Default is localhost:27017.

Example:

host1:27017,host2:27018,host3:27019

Use SSLSpecifies whether SSL should be used when connecting to the MongoDB server. If empty, the default value will be used. Default is false.
Read Preference

Specifies the preference in which the tool reads operations to the members of a replica set. See the MongoDB documentation for additional information about read preference options. Default is primary.

Authentication Settings

Authentication Mechanism

Specifies the authentication mechanism used to connect to the MongoDB server. The following values are acceptable:

none: Use this value if no authentication mechanism is required.

server-defined: Use this setting to use the default authentication method configured on the mongoDB server (SCRAM-SHA-1 or MONGODB-CR).

LDAP: Use this setting to authenticate using an LDAP service, such as Active Directory or OpenLDAP.

UsernameSpecifies the username with which to authenticate to a MongoDB database that uses authentication.
PasswordSpecifies the password with which to authenticate to a MongoDB database that uses authentication.
Authentication DatabaseSpecifies the authentication database to use. If empty, the default value will be used. This setting is ignored when the authentication mechanism is set to LDAP. Default is admin.

Query Settings

DatabaseSpecifies the name of the MongoDB database to query.
CollectionSpecifies the name of the MongoDB collection to query.
QuerySpecifies the query in the strict mode of the MongoDB Extended JSON language (see the MongoDB Documentation for details). If empty, the default value will be used. Default is all documents.
ProjectionSpecifies the projection in the strict mode of the MongoDB Extended JSON language (see the MongoDB Documentation for details). If empty, the default value will be used. Default is all fields.
SortSpecifies the sort order in which the query returns matching documents in the strict mode of the MongoDB Extended JSON language (see the MongoDB Documentation for details). Only applicable when Operation is set to find. If empty, the default value will be used. Default is unsorted.
SkipSpecifies how may documents, starting from the first, that should be skipped before returning the result of the query. Only applicable when Operation is set to find. If empty, the default value will be used. Default is 0 (nothing skipped).
LimitSpecifies the maximum number of documents should returned by the query. Only applicable when Operation is set to find. If empty, the default value will be used. Default is 0 (no limit).

Update Settings

Operation

Specifies a document handling operation. Options are:

  • find: Enter to find documents in the collection that match the query and return the projection fields.
  • insert: Enter to insert a new document from the Input tab results and return the inserted document.
  • update: Enter to update one or more documents in the collection that match the query with those from the Input tab results, then return counts of how many matched the query and how many were modified. Works in conjunction with Upsert and Multi settings described below.
  • delete: Enter to delete documents that match the query from the collection and return a count of how many were deleted. Works in conjunction with Multi setting described below.

If empty, the default value will be used. Default is Find.

Upsert

Applies only when the Operation setting described above is set to update. Enter True to insert a new document from the Input tab results when no documents match the query. When set to False, will only update existing documents and take no action when no documents match the query. If empty, the default value will be used. Default is False.

Multi

Applies only when the Operation setting described above is set to update or delete. Enter True to update or delete all documents that match the query. When set to False, will update or delete the first document that matches the query only. If empty, the default value will be used. Default is True.

Input Tab

The MongoDB Tool uses JSON input entered on this tab for insert and update operations. Input entered here is ignored for find and delete operations. See Update Settings for more information about these operations.

In the case of insert operations, the input on this tab represents the new document to be inserted into the MongoDB collection. For update operations, the input on this tab represents a command with MongoDB update operators such as $set and $inc. The MongoDB tool can be chained to the JSON output of other tools to use that JSON output as its own input (for example to insert a new document from the response of a REST API call).

Example Query 1

The following query returns all the items in the product collection.

Example Query 2

The following query returns all fields of product id 101 from the products collection.

Example Query 3

The following query returns only the sku field for product id 101 from the products collection.

Using Output Tools to View and Process Query Results

Any tools that operate on JSON can be added as outputs to a MongoDB tool. For example, you might want to add a JSON Data Bank to view the results and/or a JSON Assertor to validate the results. To add an output:

  1. Right-click the appropriate MongoDB Tool node and choose Add Output.
  2. In the Add Output dialog, choose a JSON tool in the right pane and click Finish.

After the MongoDB tool is executed, output will be passed to the chained tool. You can then perform additional tool configuration as needed (for example, to specify assertions, extractions, and so on).

Change Log

The following updates to this extension were made.

1.4

  • Added sort, limit, and skip settings to query functionality.

1.3

  • Added ability to insert, update, and delete MongoDB documents.

1.2

  • Added support for MongoDB 4.0, 4.2, 4.4.

1.1

  • Added support for multiple hosts. 

    Scripts that specify the host and/or port connection settings will not persist after upgrading the tool.

    The host and port connection settings have been merged into a single field. If you used a script to define your host and/or port connection, you will need to add a new script to configure these settings in the Hosts field after upgrading to this version. See Connection Settings for additional information.

  • Added support outputting results as traffic.
  • Added support for read preference configuration.

1.0

  • Initial release.

Third-Party Content

This plugin includes items that have been sourced from third parties as outlined below.

Additional license details are available in this plugin's licenses folder.

  • No labels