Home / All Integrations / MongoDB

free

Connect MongoDB to your chatbot

Connect with a MongoDB database from your CSML Chatbot


With this MongoDB integration, you can easily perform read and write queries on your MongoDB database directly from your chatbot.

To get started, you need a publicly-accessible MongoDB database. All recent versions of MongoDB should be compatible with this integration.

Setup

To configure this integration, provide your database connection settings in the environment variables of this App. You will need the MongoDB connection URL and the database name.

Usage

This library is based on the MongoDB driver for nodejs, and all the Collection class' methods are exposed. You can find the documentation here.

Examples

You can read more examples of all the methods on the official MongoDB nodejs driver usage examples.

findOne

// Query for a movie that has the title 'The Room'
do query = { "title": "The Room" }

do options = {
  // sort matched documents in descending order by rating
  "sort": { "rating": -1 },
  // Include only the `title` and `imdb` fields in the returned document
  "projection": { "_id": 0, "title": 1, "imdb": 1 },
}

do result = App("mongodb", method="findOne", collection="movies", query=query, options=options)

// always check that the query is successful and that there is a result!
if (result.success && result.data) {

  // the returned data is in `result.data`, so if you are looking for the description:
  say "This is the description of the movie {{query.title}}: {{result.data.description}}"
}

insertOne

Insert (insertOne, insertMany) methods take a doc argument with the document to insert

// create a document to be inserted
do doc = { "name": "Red", "town": "kanto" }

do result = App("mongodb", method="insertOne", collection="movies", doc=doc)

updateOne

Update (updateOne, updateMany) and replace (replaceOne, replaceMany) methods take a filter and a doc argument

// create a filter for a movie to update
do filter = { "title": "Blacksmith Scene" }

// this option instructs the method to create a document if no documents match the filter
do options = { "upsert": true }

// create a document that sets the plot of the movie
do updateDoc = {
  "$set": { 
    "plot": "Blacksmith Scene is a silent film directed by William K.L. Dickson."
  }
}

do result = App("mongodb", method="updateOne", collection="movies", filter=filter, doc=doc, options=options)

deleteOne

  // Query for a movie that has a title of type string
  do query = { "title": { "$type": "string" } }
  do result = App("mongodb", method="deleteOne", collection="movies", query=query)
Discover similar integrations:
Airtable
Amazon Dynamodb
Firebase
MySQL
Notion
PostgreSQL

Join the Community
on Slack Slack

Come and learn all about CSML with other chatbot enthusiasts on the CSML Community Slack! ūü§ó