Version:

LESSON

MongoDB Connector Overview


Length: 7:18 min

Version:

Description

Learn how to use the MongoDB Connector Module to query and update data from a MongoDB NoSQL database.

Video recorded using: Ignition 8.1

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. The MongoDB connector gives the ability to create a new connection type from the gateway webpage's config page, it adds a new binding type to Perspective, and adds new scripting functions. 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. To create my connection from Ignition, I'll need some information in Atlas. I can grab that by clicking on "Database" and then "Connect". I'm going to 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 4.3 or later. This connection string at the bottom contains all the info that 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.

[01:07] Those just need to be substituted for whatever user you set up to access this database. I'll also, quickly go into the sample databases I have to show a record that I want to view an ignition. Within this "sample_airbnb" database is a collection named "listingsAndReviews". I'll bring this document with the name "Ocean View Waikiki Marina w/prkg" into Ignition to view it. I'll switch over to my gateway webpage and on the config page, I'll scroll down to where it says Connectors and click MongoDB. I'll create a brand new connection and start filling in the requested fields with the information I copied from my connection string. The name is just the name of the connection in Ignition. I'll call this MongoDB Test. The connection scheme identifies the format for the connection. This is the first portion of the connection string, and I just need to append a "+srv" to the default value. I can find my connection host in the connection string after the "@", but before the "?".

[02:13] The database I'm connecting to is "sample_airbnb". I'll enter the username and password that I set up in Atlas, and finally, I'll copy over the connection properties that are at the end of the connection string. Now I can click Create New MongoDB Connector, and once that connection's valid, I can head over to my Designer to use the new Perspective binding type. I have a blank view that I'll go ahead and drop a label onto. I'll add a binding to the text property, and you can see there's a new binding type called MongoDB.

[03:15] 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, FindOne and Aggregate. I just wanna return the one result we saw earlier, so I'll choose FindOne. Now I'll use the query builder to add a filter. I'll add a key value pair where the key is called name, which is the field I want to filter on, and then the value will be Ocean View Waikiki Marina w/prkg. Now I can hit okay, and you can see that it's returning all the fields for the document that matched my filter. If we look at the Perspective Property Editor, we can see all the different fields including the name. If I wanted to only return certain fields, I could add a script, transform and filter the return value, or I can go back to the binding and add a projection. For example, if I wanted to return the name of the listing and the number of beds, I can add a new value under projection for name, and I'll set that to one.

[04:20] Then I can add another value for beds and set that to one, and then I can hit okay, and you can see that there are less fields returned from the query. 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. In addition to a Perspective binding. The MongoDB Connector Module also includes some new scripting functions, which I'll demonstrate in a component script event. 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.

[05:23] I'll choose updateOne to update one of the fields in the document we just looked at. The name of the connector I created is "MongoDB Test". The collection is "listingsAndReviews", and then the filter is going to be a PyDictionary where the keys are the field names, and then I supply the values that I want to match. In my case, I want to update the record where the name is Ocean View Waikiki Marina w/prkg. Then I need to provide my update operators.

[06:05] 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 beds field to zero. I'll quickly add a Perspective print call to this so that we can take a look at the return, and we can see that the update was acknowledged and one record was matched and modified. If I head back over to my Atlas database in my browser, I can refresh it and confirm that the document has been updated to say that there are zero beds.

[07:09] These are some quick examples of the new features that are added with the MongoDB Connector Module.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.