The Viabl.ai Deployment Server depends heavily upon Mongo (or optionally DocumentDB when using AWS). This functionality (ond optionally the system database if allowed in the deployment server settings) can be leveraged by Viabl.ai applications.
There is a system MongoDB object xrMongoDB available for use in your viabl Knowledge-Bases. This is inbuilt into the JavaScript editor and the available methods are as follows:
This function is used to perform a mongoDB aggregation.
For example, to find all documents where the productids array property contains a specified productid...
let r = xrMongoDB.aggregateDocuments(collectionName, [
{$match: {"productids": { $elemMatch : { $eq: productid} }}}
]);
For more information, see mongoDB aggregate documentation
Count the number of documents in a collection which match the query. For example:
let collectionName = "ticket_system";
let uID = #Ticket_Id.val();
let c = xrMongoDB.countDocuments(collectionName, {ticketid: uID});
If a ticket exists, c should be equal to 1. If c=0 then the ticket has not been found and if c>1 there are duplicate tickets
For more information, see mongoDB countDocuments documentation
Delete documents from a collection which match the specified query. For example:
let collectionName = "products";
let r = xrMongoDB.deleteMany(collectionName, [
{$match: {"productids": { $elemMatch : { $eq: productid} }}}
]);
Any productids that match the productid will be passed into this function as an array and all will be deleted
For more information, see mongoDB deleteMany documentation
Find (and return) all documents in a collection which match the supplied query. For example:
let collectionName = "products";
let r = xrMongoDB.findMany(collectionName, {productid: ID});
This would return a list of products.
For more information, see mongoDB find documentation
Find a single document in the specified collection which matches the supplied query. e.g. to find a product (by productid) and [if found] return it's cost...
let collectionName = "product_table";
let uID = #Product_Id.val();
r = xrMongoDB.findOne(collectionName, {productid: uID});
if (r==null) {
return "";
} else {
return r.cost;
}
For more information, see mongoDB findOne documentation
Use this method to find a document in a collection, perform an update on the document and return the new document. For example:
let settingsCollectionName = "ticket_settings";
let r2= xrMongoDB.findOneAndUpdate(
settingsCollectionName,
{key: "nextTicketNo"},
{$inc: {value: 1}},
{upsert: true, returnNewDocument: true, returnDocument: "after"}
);
The above example retrieves the next ticket number available.
For more information, see mongoDB findOneAndUpdate documentation
Insert a document into the specified collection. e.g.
let collectionName = "ID_table";
let uID = #Id.val();
let prodName = #Product_Name.val();
let r = xrMongoDB.insertOne(collectionName, {
product: uID,
productname: prodName,
createddate: new Date().toJSON()
};
Inserts a new product into a table with a product name and date product was added.
For more information, see mongoDB insertOne documentation
Specify the mongoDB location, credentials and database name for the connection.
For example:
xrMongoDB.setup({
jeremyDB: false,
url: "mongodb://name:pass@localhost",
dbname: "email_db"
});
n.b. If enabled on the deployment server, you can set the jeremyDB property to true. This allows your deployed applications to use the deployment server's system database for persistant data storage. In this scenario the url property is ignored!
Update a document (which matches the supplied query) with the properties supplied in the updateDocument. e.g. Find a product in the collection and update the cost property...
let collectionName = "products_table";
let r = xrMongoDB.findOne(collectionName, {productid: #ProductId.val()});
if (!r) {
// product doesn't exist in the database
} else {
// update product
r = xrMongoDB.updateOne(collectionName, {productid: #ProductId.val()}, {cost: #Cost.val{}});
}
n.b. The updateOne methos performs the mongoDB $set operation on the matched document.
For more information, see mongoDB updateOne documentation