MongoDB plugin
Overview
The MongoDB plugin connects QALIPSIS to MongoDb.
- Technology addressed
-
MongoDB: https://mongodb.com/
- Dependency
-
io.qalipsis.plugin:qalipsis-plugin-mongodb - Namespace in scenario
-
mongodb() - Client library
-
MongoDB Reactive Streams Driver: refer to MongoDB Java Reactive Streams.
Supported steps
Poll step
The poll step within the MongoDB plugin polls the newest records from a database collection, with a delay between each execution.
- Ancestor
-
Scenario
- Functionality
-
The
pollstep is created only once in a scenario; it is then used throughout the scenario as called. Thepollstep within the MongoDB plugin uses a strategy of "delivered at least once". - Example
-
mongodb().poll { name = "poll" connect { (1) MongoClients.create("mongodb://James23:SantaFe1987@localhost:27017") } search { (2) database = "iot" collection = "batteryState" query = Document() sort = linkedMapOf("deviceId" to Sorting.ASC) (3) tieBreaker = "deviceId" (4) } pollDelay(Duration.ofSeconds(1)) (5) }1 Create a MongoDB reactive client using the connection string. 2 Configure the search parameters. databaseandcollectionidentify the target.3 Order results by deviceIdascending.4 Configure name of the field used to track the last-seen value. 5 Wait one second between poll executions.
- Tips
-
-
Use the
pollstep with a join operator if you need to verify the values saved by your tested system in the database. -
sortandtieBreakerare both required. ThetieBreakerfield name must be the first key insort. -
The latest known value for
tieBreakeris used to filter out records already received from the database.
-
- Reference Documentation
-
Refer to the Mongodb documentation for further parameter and configuration information.
Save step
The save step within the MongoDB plugin persists a batch of documents into MongoDB.
- Ancestor
-
Step
- Functionality
-
The
savestep’s input and step context are used to generate the database name, collection name, and list ofDocumentobjects, which are then inserted into the database. - Example
-
.mongodb() (1) .save { name = "save" connect { (2) MongoClients.create("mongodb://James23:SantaFe1987@localhost:27017") } query { (3) database { stepContext, input -> (4) "iot" } collection { stepContext, input -> (5) "batteryState" } documents { stepContext, input -> (6) listOf(Document.parse(objectMapper.writeValueAsString(input))) } } }1 Wraps the preceding step into the MongoDB plugin namespace. Required before any MongoDB step. 2 Create a MongoDB reactive client using the connection string. 3 Configure the save query. All three sub-functions ( database,collection,documents) receive the step context and the input value from the previous step.4 Returns the name of the target database. Can be dynamic based on ctxorinput.5 Returns the name of the target collection. Can be dynamic based on ctxorinput.6 Builds the list of Documentobjects to insert; the input object is serialized to JSON and parsed into a BSONDocument.
- Tips
-
-
Use the
savestep after acollectstep in order to reduce the number of queries executed onto the database and to reduce the load your scenario generates on the test system. -
The
savestep is extremely flexible; database, collection and documents may depend on the context and input value received from the previous step.
-
- Reference Documentation
-
Refer to the Mongodb documentation for further parameter and configuration information.
Search step
The search step within the MongoDB plugin searches records in the database using a search query.
- Ancestor
-
Step
- Functionality
-
The
searchstep’s input and step context are used to generate a query document. The matching results are wrapped in aMongoDBSearchResultand forwarded to the next step(s). Access the result documents viaresult.documents: List<Document>. - Example
-
.mongodb() (1) .search { name = "search" connect { (2) MongoClients.create("mongodb://James23:SantaFe1987@localhost:27017") } search { (3) database { stepContext, input -> "iot" } (4) collection { stepContext, input -> "batteryState" } (5) query { stepContext, input -> (6) Document( mapOf( "deviceId" to input.deviceId, "timestamp" to input.timestamp.epochSecond ) ) } } }1 Wraps the preceding step into the MongoDB plugin namespace. Required before calling .search {}.2 Create a MongoDB reactive client using the connection string. 3 Configure the search query parameters. Each sub-function receives the step context and the input from the previous step, allowing fully dynamic queries. 4 Returns the name of the target database. 5 Returns the name of the target collection. 6 Builds the BSON Documentused as the query filter. Fields are populated from the current step input. - Tip
-
Use the
searchstep after acollect()step in order to reduce the number of queries executed onto the database and to reduce the load the scenario generates on the test system. - Reference Documentation
-
Refer to the Mongodb documentation for further parameter and configuration information.
Configuration
DSL parameters
Available parameters are described in the table below.
| Parameter | Description |
|---|---|
|
Configures the connection to MongoDb. Example
|
|
Specifies a document respecting the requirements of the target database. Example
|
|
Configures the search parameters block. The shape of the block differs between Poll and Search. Example (poll)
Example (search)
|
|
The name of the MongoDB database to target. Example (Poll)
Example (Search)
|
|
The name of the MongoDB collection to target within the database. Example (Poll)
Example (Search)
|
|
The BSON filter document used to match records. In Poll, assigned directly as a Example (Poll)
Example (Search)
|
|
Defines the ordering of results. In Poll, Example (Poll)
Example (Search)
|
|
Name of the field used to track the last-seen value for polling. Only records whose Example
|
|
Delay between two polling executions. Accepts either a Example
|
|
Configures how polled results are delivered to scenario minions. |
Shared defaults for MongoDB steps
You can define defaults once in the scenario section or just after, and let all following MongoDB steps inherit them.
scenario {
mongodb().defaults { (1)
connect {
MongoClients.create("mongodb://user:password@localhost:27017")
}
monitoring {
events = true
meters = true
}
}
}
| 1 | Defaults are applied to subsequent MongoDB steps in the same scenario. Individual steps can still override values. |