HELP!

MongoDB Access

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:

  • xrMongoDB.aggregateDocuments(collectionName, aggregationObject)
  • xrMongoDB.countDocuments(collectionName, queryObject)
  • xrMongoDB.deleteMany(collectionName, queryObject)
  • xrMongoDB.findMany(collectionName, queryObject)
  • xrMongoDB.findOne(collectionName, queryObject)
  • xrMongoDB.findOneAndUpdate(collectionName, queryObject)
  • xrMongoDB.insertOne(collectionName, document)
  • xrMongoDB.setup({url: "mongodb://name:pass@localhost", dbname: "my_db"})
  • xrMongoDB.updateOne(collectionName, queryObject, updateDocument)
Database functionality is automatically supported in Silent & Chat deployments. However if access is required under Dialog deployment then you are required to use a Scripting Object for the database integration is set to Run on Server. Alternatively a separate Silent Knowledge-base which deals with the database access can be set up to be accessed from the Dialog Knowledge-base as a Web Service.

xrMongoDB.aggregateDocuments(collectionName, aggregationObject)

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

xrMongoDB.countDocuments(collectionName, queryObject)

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

xrMongoDB.deleteMany(collectionName, queryObject)

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

xrMongoDB.findMany(collectionName, queryObject)

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

xrMongoDB.findOne(collectionName, queryObject)

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

xrMongoDB.findOneAndUpdate(collectionName, queryObject)

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

xrMongoDB.insertOne(collectionName, document)

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

xrMongoDB.setup({url: "mongodb://name:pass@localhost", dbname: "my_db"})

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!

xrMongoDB.updateOne(collectionName, queryObject, updateDocument)

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

On This Page