You can help by commenting or suggesting your edit directly into the transcript. We'll review any changes before posting them. All comments are completely anonymous. For any comments that need a reply, consider emailing docs@inductiveautomation.com.
Version:
Supplemental Videos
LESSON
MongoDB Connector Overview
Description
Learn how to use the MongoDB Connector Module to query and update data from a MongoDB NoSQL database.
Video recorded using: Ignition 8.3
Transcript
(open in window)[00:00] In this lesson, I'll demonstrate how to use the MongoDB connector module to retrieve and update data from a MongoDB NoSQL database. I have some sample data in my MongoDB Atlas database that's in the cloud. I've already gone through the effort of adding the proper network access for my cluster, and I've created some user credentials within Atlas. Before I can interact with this data in Ignition, I'll need to create a connection. I'll need some information from Atlas in order to do that. In Atlas, I can find this info by clicking on Connect next to the name of my cluster. I'm gonna select Drivers as my connection method, and then I wanna make sure that my driver is set to Java and it's set to version 5.1 or later. This connection string at the bottom contains all the info I need to make my connection from Ignition so I can copy this and reference it later. One thing to note is that it uses placeholders for the username and password, so those just need to be substituted for whatever user was set up to access this database.
[01:01] I'll also, quickly go into the sample databases to show an example of the kinds of data I'm storing. Within this sample_mflix database is a collection named "movies". We'll focus on this collection for our demonstration. I'll switch over to my Gateway webpage to create my MongoDB connection, and I'll go to Connections, Service Connectors, and click Connections. I'll create a new one and select MongoDB. I'll call this connection MongoDB, and then I'll start pulling information from that connection string we saw earlier to fill out the rest of these fields. The connection scheme identifies the format for the connection and is the first portion of the connection string. Mine needs "+srv" at the end, so I'll choose that option from the dropdown. I'll need to add a connection host, and mine can be found in the connection string after the @ sign, but before the ?. The database I'm connecting to is sample_mflix. The connection properties are found at the end of the connection string, so I'll copy those over. Finally, I'll enter the username and password that I set up in Atlas.
[02:12] Then I'll click Create Connection. Once my status says connected, I'll head over to my designer to use the new Perspective binding type. I already set up a view with a table in a numeric entry field for a user to input a year. I'll add a binding to the data property of my table and choose the MongoDB binding type. From here, I can choose from the connectors that I've created on my Gateway webpage. I can choose the collection that I want to query from, and then I can choose a query type, the options being find, find one, and aggregate. I wanna return a list of movies based on a given year, so I'll choose the movies collection and then the Find query type. In order to look for specific results, the year a movie was released, I'll use the Query Builder to add a filter. I'll add a key value pair where the key is called Year, which is the field I want to filter on, and then the value will be bound to the numeric entry field on my view.
[03:12] Now I can hit OK, and you can see that it's returning all the fields for the documents that match my filter. If we look at the Perspective Property Editor, we can see all the different fields, including the year. If I wanted to only return certain fields, I could modify the results with something like a script transform, or I can go back to the binding and add a projection. To do this, I'll provide the name of the field I'm interested in as the key, and then the value will be 1. I'll do this with a couple fields to provide a quicker look at the movies. You might have noticed the "_id" field also gets returned by default, but if I want to exclude that, I can go back to the Query builder and add another value for _id and set that to zero.
[04:04] Beyond that, this will limit the number of results by default to 10, but that can be changed in the binding. For example, I'll set it to zero so there's no limit, and now I have 713 results. In addition to a Perspective binding, the MongoDB Connector Module also includes some new scripting functions that allow you to interact with the data. I'll demonstrate in a component script event, but first, I know there's a specific movie release this year that I'd like to modify. Let me just modify the binding really quick to filter on the title. Here's the document for the movie I'm trying to modify. Let's add a button to use one of the scripting functions. These new functions can be found in the system.mongodb library. There are functions that, for example, find documents, delete documents, and insert documents, and there are also functions for listing connector or collection info. I'll choose updateOne to update one of the fields.
[05:02] In that document, I'll start filling out the required parameters. The name of the connection I created is MongoDB. The collection is movies. The filter is going to be a PyDictionary where the keys are the field names, and then I supply the values I want to match. In my case, I wanna update the record where the title is, "The Hitchhiker's Guide to the Galaxy". Then I need to provide my updates. This will also be a PyDictionary where I provide my desired operators as keys, and the values will also be PyDictionaries, including the updates themselves. I'll be using the $set operator, and then my update will be to change the plot field to 42. I'll quickly add a Perspective print call to this so that we can take a look at the return. The function returns a PyDictionary of results, and we can see that the update was acknowledged and one record was matched and modified.
[06:06] If we go back to our table and refresh, we can confirm that the document has been updated with the new plot. Perfect. These are some quick examples of the new features that are added with the MongoDB connector module.