MongoDB API
- Using the MongoDB Explorer
- Defining a Connection String (NoSQL Support)
- Low Level Xbasic Functions
- New Feature Videos
- Working with the Extension::MongoDB Class
- Creating Document Collection
- Inserting Documents (Class)
- Updating Documents (Class)
- Deleting Documents (Class)
- Retrieving data
- Advanced data Retrieval with Map/Reduce
- Optimizing Searches with Indexes
- Managing Databases and Collections
- Functions and Helpers
Description
NoSQL document databases are popular for certain types of applications. This guide covers the MongoDB Explorer, the low-level Xbasic functions, and the Extension::MongoDB class.
Discussion
MongoDB is a NoSQL database. For some time, Alpha Anywhere has allowed you to work with data in a MongoDB database by treating it as if it were a SQL database via AlphaDAO. This required creating a schema definition for the collections Connecting to MongoDB (AlphaDAO).
Now, with v4.6.5.1, a new option is available. You can use low-level Xbasic functions to interact with the MongoDB database directly. When you use this option, there is no need to define a schema.
This document covers:
- The MongoDB Explorer (GUI tool)
- The Low-level Xbasic Functions (New schema-less API)
- The Extension::MongoDB Class (Object-oriented API)
Using the MongoDB Explorer
The MongoDB Explorer can be used to perform CRUD (create, read, update and delete) operations on any collection in a MongoDB database. The MongoDB database can either be running locally or in the cloud (Mongo Atlas).
To open the MongoDB Explorer, select the MongoDB Explorer... command from the Tools menu when the Web Control Panel has focus.
The MongoDB Explorer allows you to:
- View documents in a MongoDB collection using a paginated view
- Set the page size and page number for the document viewer
- View data in either JSON format or tabular format
- Create databases and collections
- Delete databases and collections
- Edit, Add, or Delete documents in a collection
- Query a collection using SQL syntax (specify a filter and/or an order)
- See the corresponding native MongoDB syntax for a SQL filter or Order By clause
- Import data from a SQL database into a new MongoDB collection
Defining a Connection String (NoSQL Support)
To define a Mongo connection string, click the SQL button on the toolbar when the Web Control Panel has focus. Select AlphaDAO Connection strings, click New, specify a name, and click Build. Select MongoDB from the API list.
Select the URL for the MongoDB service:
- Local: //localhost:27017
- Atlas/Cloud: //<userName>:<password>@cluster0.wrchfwt.mongodb.net/
When creating the connection string, you can now specify that you want to use low level Xbasic functions to interact with your Mongo database. This is faster because Alpha Anywhere does not translate SQL statements into low level Mongo commands.
Low Level Xbasic Functions
The following Xbasic functions allow you to interact with MongoDB directly. Click the links below for syntax and examples.
- mongo_deleteDocument - delete a document (specified by document Id) from a collection.
- mongo_createDocument - create a new document in a collection. Returns the Id of the new document.
- mongo_updateDocument - update a document.
- mongo_getDocument - retrieve a document (specified by document Id).
- mongo_query - execute a query to find documents in a collection.
- mongo_listDatabases - list the databases in a MongoDB server.
- mongo_listCollections - list the collections in a database.
- mongo_deleteCollection - delete a collection in a database.
New Feature Videos
MongoDB Explorer and Atlas
- Watch Video: Connecting to Mongo Atlas
- Watch Video: Connecting to Local Mongo with Schema
- Watch Video: Creating a Database
- Watch Video: Importing Data
- Watch Video: Pagination and Navigation
- Watch Video: Querying Data
- Watch Video: Adding New Records
Working with the Extension::MongoDB Class
The older methods below belong to the extension::MongoDB class. These provide an object-oriented way to manage databases and are distinct from the low-level functions listed above.
Creating Document Collection
If you already have a Mongo database server, creating new documents in a Mongo database is very easy. If the database doesn't already exist, and you have permission to create a database, a new database will automatically be created for you when you try to add a collection to the database. The same goes for collections. The creation won't occur until you execute your first command against the data.
Inserting Documents (Class)
To add records to a Mongo database, simple call the AddRecord() method with a properly formatted JSON string. If the record was added, the value of the unique _id that was assigned to the document is returned.
dim Mongo as extension::MongoDB = extension::MongoDB::Create("mongodb://localhost:27017","MyNewDatabase","MyNewTable")
? Mongo.AddRecord(<<%str%
#{ "Firstname" : "John" , "Lastname" : "Public" , "Date" : "10/8/1988" } #%str%) = "53ac89b37116e1d426bd8c95"
? Mongo.AddRecord(<<%str%
#{ "Firstname" : "Jane" , "Lastname" : "Doe" , "Date" : "10/8/1988" }
#%str%)
= "53ac89c17116e1d426bd8c96"
? json_reformat( Mongo.GetAllRecords() , .t. )Updating Documents (Class)
If you want to modify the contents of an existing document, you will need its ID and the new content using UpdateRecord().
dim Mongo as extension::MongoDB = extension::MongoDB::Create("mongodb://localhost:27017","MyNewDatabase","MyNewTable")
Mongo.UpdateRecord("53ac89c17116e1d426bd8c96",<<%str%
#{ "Firstname" : "Janet" , "Lastname" : "Doe" , "Date" : "11/5/1976" }
#%str%)
? json_reformat( Mongo.GetAllRecords() , .t. )Deleting Documents (Class)
Document deletion merely requires calling the DeleteRecord() method passing the documents unique id.
dim Mongo as extension::MongoDB = extension::MongoDB::Create("mongodb://localhost:27017","MyNewDatabase","MyNewTable")
Mongo.DeleteRecord("53ac89c17116e1d426bd8c96")
? json_reformat( Mongo.GetAllRecords() , .t. )Retrieving data
Getting All documents using in a Mongo db collection is done by calling the GetAllRecords() method.
dim Mongo as extension::MongoDB = extension::MongoDB::Create("mongodb://localhost:27017","AppServerDemo","flowers")
? json_reformat( Mongo.GetAllRecords() , .t. )Getting a single document is another common method, which requires you pass the internal "id" for a record to GetRecord().
dim Mongo as extension::MongoDB = extension::MongoDB::Create("mongodb://localhost:27017","AppServerDemo","flowers")
? json_reformat( Mongo.GetRecord("5399ddd502681b9c258dc5f8") , .t. )Retrieving documents using criteria, a subset of the fields in the document, or retrieving the documents require using the Mongo-specific GetRecords() function.
dim Mongo as extension::MongoDB = extension::MongoDB::Create("mongodb://localhost:27017","AppServerDemo","flowers")
? json_reformat( Mongo.GetRecords(<<%str%
#{ "Imagedate": "05/02/2003" }
#%str%), .t. )Advanced data Retrieval with Map/Reduce
In addition to the GetRecords function, there is a MapReduce() function on the Mongo object that provides the means to filter and compose data using server-side Javascript functions.
dim Mongo as extension::MongoDB = extension::MongoDB::Create("mongodb://localhost:27017","AppServerDemo","flowers")
? json_reformat( Mongo.MapReduce("function() {emit(this.Imagedate,this.Keywords);}","") , .t. )Optimizing Searches with Indexes
Mongo provides the means to index fields in collections. To see what indexes already exist, call the GetIndexes() method.
To add a new index, call the CreateIndex() method. A smarter index add function exists if you want to have the function first test to see if the index already exists, this function is called EnsureIndex().
dim Mongo as extension::MongoDB = extension::MongoDB::Create("mongodb://localhost:27017","AppServerDemo","flowers")
Mongo.EnsureIndex(<<%str%
#{ "Keywords" : 1 }
#%str%,"")Managing Databases and Collections
There are a number of utility functions for managing databases and collections.
- To get a List of all the databases in a Mongo server instance, call the ListDatabases() function.
- To delete a databases (and all collections in the database) call the DropDatabase() function.
- To get a list of all the collections in a databases, call the ListCollections() method.
- To Remove a collection (and all its documents) call the DropCollection() method.
- Collections can be renamed using the RenameCollection() method.
Functions and Helpers
Mongo databases can have saved functions, which are similar to stored procedures in SQL server. You can use ListFunctions() and FunctionAdd() to manage them.
Alpha Anywhere also includes a helper function CriteriaToJavascript that allows a developer to create the javascript that returns the same records as a view definition would.
' Simple exact match
dim view as c = <<%str%
#{ "person": "jim" }
#%str%
? extension::MongoDB::CriteriaToJavascript(view)
= doc.person === "jim"See Also

