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

I’ve continued doing research and discovered that MongoDB used to have a REST interface, but that it was removed at version 2.6. This is why the main address of MongoDB has a resource name: mongodb://localhost:27017 . This address is apparently for use only internally by MongoDB drivers and doesn’t support HTTP.

So I seem to be stuck. I wanted a simple programming interface to MongoDB like the mySQL interface, but apparently it is only available by adding lots more software, and possibly by having to use Atlas, which is MongoDB in a cloud owned by a third party, having unknown security.

I can try to see how hard it is to get the PHP driver working, but my past experience with specialized drivers on PHP is that they are difficult to obtain and get working.

So I’m reluctantly being driven to do one of two things: first, to use MySQL to store JSON, which feels overwhelming due to handling each of the JSON internal datatypes somehow, or, second, to write my own database functions to store each JSON document in a hash table inside a single file, for ease of backup and restore, with indexes for each searchable object property. That means a very large file that I’ll index in pieces, so the whole file doesn’t have to be resident in memory. In any case, another subproject for my already late project.

Any further suggestions from anyone? If not, this will be my last posting before giving up on MongoDB for my small business application that I want written in the standard languages (HTML, CSS, JavaScript, and PHP).

Hi @David_Spector

It seems that we are in a bind. If I understand the summary so far:

  1. You wanted to write a Javascript app, but don’t want to use Node/NPM
  2. You wanted to access a local MongoDB deployment from the browser, which is not possible to do without extra software, but you don’t want to install extra software
  3. You need a database that can be accessed using only a browser, with no middleware

Please let me know if I misunderstand something :slight_smile:

Since MongoDB server interfaces to the outside world using a specific protocol, out of the box, you won’t be able to do what you want. There are other ways to do it, like:

  1. If you don’t like NPM, there are alternatives like yarn. However you still have to install something
  2. If you want to access a MongoDB server using HTTP (a protocol the server doesn’t support), you may be able to use something like resthreart that provides an API layer between your browser and the server
  3. Unless you’re writing a single page application that runs 100% inside the browser, using a local application server might do the trick as well. That is, your application lives in a local server that serves the interface web pages, and it uses a driver to connect to a local MongoDB

Since you mentioned you want to use PHP, I think option 3 is the easiest for your case. Take a look at the PHP driver. There’s a PHP quickstart article and video to get you started.

Best regards
Kevin

2 Likes

Kevin, Thanks so much for your useful information. I looked at your RESTHeart link and it certainly appears to be a solution to my problem. They even offer programming support via chat. Your PHP tutorial link is also potentially very helpful, so I will have two methods for PHP access to MongoDB to choose from.

I’m a bit worried about all this software overhead, since my application only currently has 4000 records, so they could fit into memory. For example, for the PHP case, I will have to install PHP Version 8, as I am currently using version 7. That could turn out to be a big hassle, as was my previous conversion from version 5 to version 7.

Since there was no off-the-shelf solution, I’ve started working on my own little PHP database for JSON objects, stored as text and with checksums/digests and backup to ensure data integrity. I’ll generate a test database and see how efficient it is for my purpose (a business management tool). I can then add binary search or hashing to make it more efficient, if needed. I’m not really sure what all the advantages of using MongoDB are, but perhaps I don’t need that much infrastructure just for this one application. I don’t need concurrent access, atomic transactions, or asynchronous operations.

(And MongoDB has limitations, too. For example, it doesn’t include inner join, but I probably can do without separate tables and joins in my application.)

Thanks again for your help. I will visit MongoDB again if I discover that I need more than just a simple in-memory DB module.

1 Like

Perhaps that Window.sessionStorage - Web APIs | MDN is even sufficient since you are doing it in the browser.

Permanently store in files, when needed one document, look into sessionStorage if not present load from file. Implement copy on write.

Thanks for the idea. If I’m committed to using PHP, I can also use PHP session storage, or fread and fwrite direct to file.

1 Like

Absolutely, I shared the JS version because that is the API I know. The point was really to use sessionStorage as cache for the disk storage.

I did understand that, thanks.

1 Like

@David_Spector It seems to me that you don’t fully understand the tools.

Some of the tools available are for debugging some are for writing solutions.

Learn a language that has a client available to connect to the mongodb instance if you want a solution for production.

JavaScript is quite easy and has a lot of examples using plain mongodb, mongoose or other tools which are well documented and battle tested.

Node is quite simple to getting to start with. There is a reason for all these tools.

So either learn to program either employ a developer, no shortcuts for this one I am afraid.

Hey to make it very straight forward I would suggest just using standalone packages from MongoDB.

I get your concern about not loading npm packages as you said it may introduce some packages which you really don’t need.

Check something like this

const { MongoClient } = require('mongodb');
// or as an es module:
// import { MongoClient } from 'mongodb'

// Connection URL
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);

// Database Name
const dbName = 'myProject';

async function main() {
  // Use connect method to connect to the server
  await client.connect();
  console.log('Connected successfully to server');
  const db = client.db(dbName);
  const collection = db.collection('documents');

  // the following code examples can be pasted here...

  return 'done.';
}

main()
  .then(console.log)
  .catch(console.error)
  .finally(() => client.close());

Reference is taken from mongodb
This is the native package u can run it as a js file.

Another alternative if you don’t want to launch your npm package manager everytime try make a cdn for the same and use it wherever u need and you want to run.

@David_Spector You can use this even https://www.mongodb.com/docs/v5.0/tutorial/write-scripts-for-the-mongo-shell/

Thank you so much for your help. Unfortunately, I may not have posted everywhere that I gave up on MongoDB. It is too complex for my simple JSON needs and requires the use of software tools I don’t need for programming in HTML/PHP/JavaScript.

I decided to create my own JSON DB program, which is a simple Class with methods like OpenDB and ReadRecord. It includes checking against a schema and md5 checking for record corruption and automatic backup. It is fun, is coming along fast, and will be much easier to use than MongoDB for small DBs (fewer than about 50,000 records) for my business software. Since it will work inside a browser (using a tiny local server), it should work on all devices and browsers. So far it creates a simple test case of 20,000 records in under one second using PHP 7, so it’s fast enough for local use.

So, no more help, please, and thanks.