Getting MongoDB Database And Collection Names From The Connection String

Getting MongoDB Database And Collection Names From The Connection String

Ben Nadel looks at how to extract MongoDB database and collection names from an ENV-based connection string such that the document database can be consumed more opaquely in Lucee CFML

At InVision, I'm working with Boaz Ruck on a little experiment: whereas our ColdFusion application would normally construct a MongoDB connection using a variety of individual values, the Data Services team would like to try providing the entire MongoDB connection configuration as a single environment variable. This way, the Data Service team could tweak the settings without having to touch the application code. This posed an interesting problem because the ColdFusion application is generally tightly coupled to the MongoDB implementation. As such, I needed to figure out how to access a MongoDB Collection using the Java driver without being able to hard-code the database name or the collection name. After some trial-and-error, I figured out how to extract this information from the ConnectionString object in Lucee CFML

So, normally, when creating and consuming a MongoDB document collection, my ColdFusion code will "hard code" the name of the database and the name of the collection:

mongoClient.getDatabase( "bennadel" ).getCollection( "friends" )

ASIDE: In some cases, the name of the MongoDB database is passed-in as a separate environment variable. It depends on how old the code is; newer code tends to be a bit less hard-coded.

However, the Data Services team would like to try passing all of that information in as a single environment variable:


In the ColdFusion code, however, I still have to make a .getDatabase(name) call followed by a .getCollection(name) call before I can start reading and writing MongoDB documents. Which means, my code still has to reference a database name and a collection name. It doesn't appear that I can get this information from the MongoClient client; but, if I construct a ConnectionString class from the environment variable, I can then extract the database and collection names from the connection string. This would allow me to consume then in an opaque manner in my Lucee CFML


Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

MySQL,CFQueryParam,Prepared Statements And SQL Queries That Use IN Clauses In ColdFusion

Ben Nadel takes a moment to reflect on his mental model prepared statements in MySQL. And how he might use the CFQueryParam tag to go about safely creating prepared statements for SQL queries that use the IN clause with a dynamic set of parameters.

Evaluating Database Records That Contain Interpolation Expressions

Ben Nadel has fun dynamically evaluating ColdFusion String interpolation expressions that are stored in a database using Adobe ColdFusion 2018. This demo uses the evaluate() and de() functions to merge runtime variables into a database-driven value.

Using Constants To Help Clarify Boolean Arguments

Ben Nadel continues to noodle on the use of Boolean flags in ColdFusion and JavaScript; and how using named constants can bring some much appreciate clarity to otherwise complex code. This can be done in both ColdFusion / Lucee CFML as well as JavaScript - my two main languages. And, I wanted to share a quick demo.

I've Had Fundamental Misunderstandings Of How CFQueryParam Works

Ben Nadel realizes that he's had some pretty large, fundamental misunderstandings of how prepared statements are parsed, cached, and executed when using the MySQL JDBC Driver in his ColdFusion / Lucee / CFML applications. And what implications this has for his use of the CFQueryParam tag.

Testing wkhtmltopdf 0.12.6 With Docker In Lucee CFML

Ben Nadel looks at using the wkhtmltopdf project in Lucee CFML to generate PDF documents from dynamic, ColdFusion code. He performs his experiment using the Ortus Solutions' CommandBox Docker container.