MongoDB Steps

Configuration

In order to simplify steps configuration, two parameters can be set to kest global configuration :

  • connection: String // Mongo DB connection string, if not set will default to mongodb://localhost:27017

  • database: String // name of targeted database, if not set will default to test

sample

kest.yml
mongodb:
    connection: mongodb://localhost:27017
    database: test

Clean database

cleanMongoDatabase

Parameters configure step

  • connection: String // Mongo DB connection string. Optional, defaults to connection configured in kest.yml

  • database: String // Database name to clean. Optional, defaults to database configured in kest.yml

  • collections: List<String> // Target collections to clean. Optional, by default all collections of DB will be cleaned

  • except: List<String> // Target collections KEEP. Optional, by default no collections of DB will be kept

Samples

cleanMongoDatabase(name = """clean "vehicles" and "planets" collections on "starwars" DB""") {
    connection = "mongodb://agalaxyfarfaraway:1300"
    database = "starwars"
    collections("vehicles", "planets")
}
cleanMongoDatabase(name = """clean all collections except "vehicles" and "planets" collections on "starwars" DB""") {
    connection = "mongodb://agalaxyfarfaraway:1300"
    database = "starwars"
    except("vehicles", "planets")
}
cleanMongoDatabase(name = """clean "vehicles" and "planets" collections on globally configured database""") {
    collections("vehicles", "planets")
}
cleanMongoDatabase(name = "clean all collections on globally configured database")

Insert document

insertMongoDocument

Parameters to provide on step

  • connection: String // Mongo DB connection string. Optional, defaults to connection configured in kest.yml

  • database: String // Targeted database name. Optional, defaults to database configured in kest.yml

  • collection: String // mandatory, collection on which insert document

  • document: String // mandatory, document to insert

Samples

insertMongoDocument(name = """insert Endor into "planets" collection on "starwars" DB""") {
    connection = "mongodb://agalaxyfarfaraway:1300"
    database = "starwars"
    collection = "planets"
    document = """
        {
            "name": "Endor",
            "population": 7500000000
        }
    """
}
insertMongoDocument(name = """insert Endor into "planets" collection on globally configured database""") {
    collection = "planets"
    document = """
        {
        "name": "Endor",
        "population": 7500000000
        }
    """
    }

Update documents

updateMongoDocument

Parameters to provide on step

  • connection: String // Mongo DB connection string. Optional, defaults to connection configured in kest.yml

  • database: String // Targeted database name. Optional, defaults to database configured in kest.yml

  • collection: String // mandatory, collection on which insert document

  • filter: Map<String, Any> // optional, filters documents to update

  • update: Map<String, Any> // mandatory, filters documents to update

Samples

updateMongoDocument(name = """update Endor planet from "planets" collection on "starwars" DB""") {
    connection = "mongodb://agalaxyfarfaraway:1300"
    database = "starwars"
    collection = "planets"

    filter = mapOf("name" to "Endor")
    update = mapOf("oceans" to 3)
}
updateMongoDocument(name = """update Endor planet from "planets" collection on globally configured database""") {
    collection = "planets"

    filter = mapOf("name" to "Endor")
    update = mapOf("oceans" to 3)
}

Read documents

givenMongoDocuments

Parameters to provide on step

  • connection: String // Mongo DB connection string. Optional, defaults to connection configured in kest.yml

  • database: String // Targeted database name. Optional, defaults to database configured in kest.yml

  • collection: String // mandatory, collection on which insert document

  • filter: Map<String, Any> // optional, filters documents to retrieve

Samples

givenMongoDocuments(name = """read Endor planet from "planets" collection on "starwars" DB""") {
    connection = "mongodb://agalaxyfarfaraway:1300"
    database = "starwars"
    collection = "planets"

    filter = mapOf("name" to "Endor")
}
givenMongoDocuments(name = """read Endor planet from "planets" collection on globally configured database""") {
    collection = "planets"

    filter = mapOf("name" to "Endor")
}

Count documents

givenCountOfMongoDocuments

Parameters to provide on step

  • connection: String // Mongo DB connection string. Optional, defaults to connection configured in kest.yml

  • database: String // Targeted database name. Optional, defaults to database configured in kest.yml

  • collection: String // mandatory, collection on which insert document

  • filter: Map<String, Any> // optional, filters documents to retrieve

Samples

givenCountOfMongoDocuments(name = """count documents from "planets" collection on "starwars" DB""") {
    connection = "mongodb://agalaxyfarfaraway:1300"
    database = "starwars"
    collection = "planets"
}
givenCountOfMongoDocuments(name = """count documents from "planets" collection collection on globally configured database""") {
    collection = "planets"
}

Delete documents!

deleteMongoDocuments

Parameters to provide on step

  • connection: String // Mongo DB connection string. Optional, defaults to connection configured in kest.yml

  • database: String // Targeted database name. Optional, defaults to database configured in kest.yml

  • collection: String // mandatory, collection on which delete documents

  • filter: Map<String, Any> // optional, filters documents to delete

Samples

deleteMongoDocuments(name = """remove planet Alderaan from "planets" collection on "starwars" DB""") {
    connection = "mongodb://agalaxyfarfaraway:1300"
    database = "starwars"
    collection = "planets"

    filter = mapOf("name" to "Alderaan")
}
deleteMongoDocuments(name = """remove planet Alderaan from "planets" collection on globally configured database""") {
    collection = "planets"

    filter = mapOf("name" to "Alderaan")
}