MongoDB
MongoDB
The MongoDB module is responsible for connecting to a MongoDB database, performing CRUD operations and handling the results inside the robot.
const connection = await Database.MongoDB.connection["v1_0_0"]({
connectionString: 'mongodb://userRoberty:robertyBD123@localhost:27017',
})
Methods
connection
Method responsible for creating a connection with a MongoDB database.
const connection = await Database.MongoDB.connection["v1_0_0"]({
connectionString: 'mongodb://userRoberty:robertyBD123@localhost:27017',
})
Required parameters
connectionString: String - connection string with the MongoDB database. The standard must be followed as in the example shown earlier.
Optional parameters
This method has no optional parameters.
Return
The constant or variable created, such as the connection of the example shown earlier, receives the connection object (MongoClient), which can be used as a parameter in the other methods.
selectDb
Method responsible for selecting a database from a MongoDB connection.
const db = await Database.MongoDB.selectDb["v1_0_0"]({
connection: connection,
dbName: 'meuBancoDeDados',
})
Required parameters
connection: Return of the connection action - expects the connection obtained through the connection action.dbName: String - name of the database to be selected.
Optional parameters
This method has no optional parameters.
Return
The constant or variable created, such as the db of the example shown earlier, receives the database object (Db), which can be used as a parameter in the other methods.
selectCollection
Method responsible for selecting a collection inside a MongoDB database.
const collection = await Database.MongoDB.selectCollection["v1_0_0"]({
db: db,
collectionName: 'minhaColecao',
})
Required parameters
db: Return of the selectDb action - expects the database obtained through the selectDb action.collectionName: String - name of the collection to be selected.
Optional parameters
This method has no optional parameters.
Return
The constant or variable created, such as the collection of the example shown earlier, receives the collection object (Collection), which can be used as a parameter in the other methods.
insertOne
Method responsible for inserting a single document into a MongoDB collection.
const result = await Database.MongoDB.insertOne["v1_0_0"]({
collection: collection,
document: { nome: 'Roberty', idade: 30 },
})
Required parameters
collection: Return of the selectCollection action - expects the collection obtained through the selectCollection action.document: Object - object to be inserted into the collection.
Optional parameters
session: Return of the startTransaction action - transaction session obtained through the startTransaction action.
Return
The constant or variable created, such as the result of the example shown earlier, receives an object with the following property:
result.insertedId: any - ID of the document inserted.
insertMany
Method responsible for inserting multiple documents into a MongoDB collection.
const result = await Database.MongoDB.insertMany["v1_0_0"]({
collection: collection,
documents: [
{ nome: 'Roberty', idade: 30 },
{ nome: 'Automation', idade: 5 },
],
})
Required parameters
collection: Return of the selectCollection action - expects the collection obtained through the selectCollection action.documents: Object[] - array of objects to be inserted into the collection.
Optional parameters
session: Return of the startTransaction action - transaction session obtained through the startTransaction action.
Return
The constant or variable created, such as the result of the example shown earlier, receives an object with the following property:
result.insertedIds: any[] - array with the IDs of the documents inserted.
findOne
Method responsible for searching for a single document in a MongoDB collection.
const result = await Database.MongoDB.findOne["v1_0_0"]({
collection: collection,
findFilter: { nome: 'Roberty' },
})
Required parameters
collection: Return of the selectCollection action - expects the collection obtained through the selectCollection action.findFilter: Object - search filter in the MongoDB object format.
Optional parameters
session: Return of the startTransaction action - transaction session obtained through the startTransaction action.
Return
The constant or variable created, such as the result of the example shown earlier, receives an object with the following properties:
result.document: Object - document found. It isnullif no document is found.result.found: Boolean - indicates whether the document was found.
findMany
Method responsible for searching for multiple documents in a MongoDB collection.
const result = await Database.MongoDB.findMany["v1_0_0"]({
collection: collection,
findFilter: { idade: { $gte: 18 } },
})
Required parameters
collection: Return of the selectCollection action - expects the collection obtained through the selectCollection action.findFilter: Object - search filter in the MongoDB object format.
Optional parameters
session: Return of the startTransaction action - transaction session obtained through the startTransaction action.
Return
The constant or variable created, such as the result of the example shown earlier, receives an array of objects with the documents found.
updateOne
Method responsible for updating a single document in a MongoDB collection.
const result = await Database.MongoDB.updateOne["v1_0_0"]({
collection: collection,
filter: { nome: 'Roberty' },
updateFilter: { $set: { idade: 31 } },
})
Required parameters
collection: Return of the selectCollection action - expects the collection obtained through the selectCollection action.filter: Object - filter to locate the document to be updated.updateFilter: Object - update object in the MongoDB format (e.g.{ $set: { campo: valor } }).
Optional parameters
session: Return of the startTransaction action - transaction session obtained through the startTransaction action.
Return
The constant or variable created, such as the result of the example shown earlier, receives an object with the following property:
result.modified: Boolean - indicates whether the document was modified.
updateMany
Method responsible for updating multiple documents in a MongoDB collection.
const result = await Database.MongoDB.updateMany["v1_0_0"]({
collection: collection,
filter: { ativo: false },
updateFilter: { $set: { ativo: true } },
})
Required parameters
collection: Return of the selectCollection action - expects the collection obtained through the selectCollection action.filter: Object - filter to locate the documents to be updated.updateFilter: Object - update object in the MongoDB format (e.g.{ $set: { campo: valor } }).
Optional parameters
session: Return of the startTransaction action - transaction session obtained through the startTransaction action.
Return
The constant or variable created, such as the result of the example shown earlier, receives an object with the following property:
result.modifiedCount: Number - number of documents modified.
deleteOne
Method responsible for removing a single document from a MongoDB collection.
const result = await Database.MongoDB.deleteOne["v1_0_0"]({
collection: collection,
findFilter: { nome: 'Roberty' },
})
Required parameters
collection: Return of the selectCollection action - expects the collection obtained through the selectCollection action.findFilter: Object - filter to locate the document to be removed.
Optional parameters
session: Return of the startTransaction action - transaction session obtained through the startTransaction action.
Return
The constant or variable created, such as the result of the example shown earlier, receives an object with the following property:
result.deleted: Boolean - indicates whether the document was removed.
deleteMany
Method responsible for removing multiple documents from a MongoDB collection.
const result = await Database.MongoDB.deleteMany["v1_0_0"]({
collection: collection,
findFilter: { ativo: false },
})
Required parameters
collection: Return of the selectCollection action - expects the collection obtained through the selectCollection action.findFilter: Object - filter to locate the documents to be removed.
Optional parameters
session: Return of the startTransaction action - transaction session obtained through the startTransaction action.
Return
The constant or variable created, such as the result of the example shown earlier, receives an object with the following property:
result.deletedCount: Number - number of documents removed.
countDocuments
Method responsible for counting the number of documents in a MongoDB collection that match a filter.
const result = await Database.MongoDB.countDocuments["v1_0_0"]({
collection: collection,
findFilter: { ativo: true },
})
Required parameters
collection: Return of the selectCollection action - expects the collection obtained through the selectCollection action.findFilter: Object - filter to locate the documents to be counted.
Optional parameters
session: Return of the startTransaction action - transaction session obtained through the startTransaction action.
Return
The constant or variable created, such as the result of the example shown earlier, receives an object with the following property:
result.count: Number - number of documents matching the filter.
aggregation
Method responsible for executing an aggregation pipeline in a MongoDB collection.
const result = await Database.MongoDB.aggregation["v1_0_0"]({
collection: collection,
pipeline: [
{ $match: { ativo: true } },
{ $group: { _id: '$categoria', total: { $sum: 1 } } },
],
})
Required parameters
collection: Return of the selectCollection action - expects the collection obtained through the selectCollection action.pipeline: Object[] - array of stages of the MongoDB aggregation pipeline.
Optional parameters
session: Return of the startTransaction action - transaction session obtained through the startTransaction action.
Return
The constant or variable created, such as the result of the example shown earlier, receives an object with the following property:
result.aggregation: AggregationCursor - cursor with the results of the aggregation.
startTransaction
Method responsible for starting a transaction in a MongoDB connection.
const session = await Database.MongoDB.startTransaction["v1_0_0"]({
connection: connection,
})
Required parameters
connection: Return of the connection action - expects the connection obtained through the connection action.
Optional parameters
This method has no optional parameters.
Return
The constant or variable created, such as the session of the example shown earlier, receives an object of the ClientSession type that can be passed as the session parameter in the CRUD actions.
commitTransaction
Method responsible for committing a transaction started in a MongoDB connection.
await Database.MongoDB.commitTransaction["v1_0_0"]({
session: session,
})
Required parameters
session: Return of the startTransaction action - expects the session obtained through the startTransaction action.
Optional parameters
This method has no optional parameters.
Return
This method has no return.
abortTransaction
Method responsible for rolling back a transaction started in a MongoDB connection.
await Database.MongoDB.abortTransaction["v1_0_0"]({
session: session,
})
Required parameters
session: Return of the startTransaction action - expects the session obtained through the startTransaction action.
Optional parameters
This method has no optional parameters.
Return
This method has no return.