(Newcomer) How do I write real JavaScript programs for Mongo/Compass?

I think that MongoDB meets my needs for tracking business clients and their orders, with fast development time. But I have hit a roadblock in the documentation and can’t find what I need. It seems to focus on data and queries, but not programming.

Can anyone please advise me how to run full-scale JavaScript programs that interface with my MongoDB, running only locally on a Windows server? Is there a Compass command or other command to run such programs? Is there a menu system for running such programs? How do I debug such programs, given that they are not running in a browser, so there are no Developer Tools and not even a Console?

Or would I be better off using PHP and the MongoDB library code? No, I don’t want to run using Smalltalk, Ruby on Rails, React, C++, Python, or any other language or framework. Just JavaScript, please. With some sort of debugging tools, please.

Also, can Mongo/JavaScript do asynchronous programming? Can I “await” a Mongo Promise to do a query or update while my program does something else until the Promise resolves?

1 Like

Look at https://learn.mongodb.com/ and follow the developer path of the language of your choice.

Thank you for taking the time to answer, but these course cover only Node, Python, C#, and Java.

And what is Node if not JavaScript?

Node is a little operating system with many dependencies that I don’t want on my computer. I especially don’t want NPM, which is an enormous library of software I don’t need.

Compass includes MONGOSH, which is all of standard JavaScript .

How do you do JS (except for web browser) without node?

I am pretty sure you may use the JS driver without node just list any JS package.

Yes. You may write scripts and use them in mongosh. See

load(‘test.js’)

MongoshUnimplementedError: [COMMON-90002] load is not currently implemented for this platform

Compass contains a complete Mozilla implementation of JavaScript, supposedly. I don’t want node on my computer, I want to develop with JavaScript, including debugging, preferably inside a browser so I have the Developer Tools available.

Yep. The Compass version of mongosh does not support load. You need to use the command line version.

I will try. But is there any way to get access to MongoDB, community version, from the JavaScript running in Firefox or Chrome? It would be ideal to do my development locally in a browser, so I have access to Console and other Developer Tools.

I tried to install the MongoDB Shell (mongosh) using the instructions at instructions and failed. First, I downloaded the zip file for Windows 64. This expanded into a folder containing a file mongosh.1.gz and a few other files. The instructions say to expand this .gz file, so I used 7-zip to expand it. The file inside, mongosh.1, was not of any known file type, so 7zip could not expand it further. Windows does not know what to do with such a file.

I can understand why a Windows installation would not be as easy as a Linux installation, given that few developers work on Windows. But this installation really does seem to fail.

Any reasons why you decided to follow the complicated way?

From the instructions link you provided follow the instructions:

Install from MSI

1

Open the MongoDB Shell download page.

Open the MongoDB Download Center.

2

In the Platform dropdown, select Windows 64-bit (8.1+) (MSI)

3

Click Download.

4

Double-click the installer file.

5

Follow the prompts to install mongosh .

No, no particular reason. When I looked there, I only saw a .zip file download. Thanks for this.

ADDED: Ah, I see the problem. There are several selection boxes. The last one is Package. If it is set to Zip it only offers Zip as a choice. It is necessary to open the Platform box, and near the end of the list is Windows/msi, which is the installation option that works. The way the download dialog works could be improved a bit.

I wrote the program test.js as follows:

alert('ok');

The result of loading this program into mongosh:

test> load('test.js')
ReferenceError: alert is not defined

Also, as I predicted, Developer Tools (including Console) are not available in mongosh.

I would like to write complete programs to make use of the MongoDB, but this does not seem easy in mongosh. Any suggestions?

Can I connect to MongoDB from regular JavaScript or PHP in a browser?

I do not agree with

Because it is easy. But don’t expect to use browser specific features. For example, there is no DOM outside the browser.

If you use features specific to the browser do not expect to run your code anywhere else then in the browser. The main issue here is that you want

but want to stick to the browser and not use npm.

I really cannot help further so this will be my last reply on this subject.

Thank you so much for your help, bye!

So, I definitely need to run in a browser, since I want access to the DOM so that I can interface with myself as the user of the database via forms.

I’m looking for a way to interface with MongoDB from JavaScript running in a browser, A starting point is the command reported by standalone mongosh:

mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2

I can certainly run this from PHP code running in a local browser using either GET or PUT, so I should be able to connect to the DB this way. Now, how do I retrieve a connection resource/pointer/object and use HTTP commands to do DB operations? Must I use the PHP library for MongoDB, or can I connect directly?

Take a look at https://www.mongodb.com/docs/atlas/api/data-api/.

Note that the js driver is a library, if somehow you are able to put the library where the browser can see it you might be able to import it with a <script> tag.

Thank you for continuing to reply even though you cannot help me.

The page you linked to makes it clear that the data api is normally disabled, and can only be enabled for Atlas, which is MongoDB in the cloud, whose data and programs are owned by whoever maintains MongoDB. As I’ve said above, I’m using the Community MongoDB, not Atlas. The reason is that I want to guarantee my clients security by not putting their data on the Web, in the cloud, or under the protection of a third party. Instead, I run the current mySQL database and its programs on a local Windows server having no access from outside of my local wireless network. I want to do the same with MongoDB and switch from relational records to JSON.

So, I still need someone to help me understand how to access MongoDB using REST (HTTP commands). I do not need all the fancy caching and other features provided by the various libraries for MongoDB, as my data is simple, and limited to just a few thousand records with no concurrent access needed. I want to use MongoDB under the assumption that it stores the data more efficiently than I could store it using my own hashing algorithms.