no-framework

How to use MongoDB in a vanillaJS project


I want to implement a CRUD mongoDB functionality into my web app witch only has HTML,CSS and vanilla JS.


Solution

  • You can't , but more important is that you shouldn't.

    MongoDB is a server database and you should connect to it with some kind of backend. So, you need to use something like Java, PHP, Node, etc. to connect to MongoDB. The right way to do this is to build some API that you can consume using JS (like a REST endpoint).

    Even if you could find a way to connect to MongoDB using JS, you would be exposing YOUR WHOLE DATABASE to anyone who has a web browser and access to the URL. Also, if you want it to be accesible from anywhere, you would need to allow ANY IP address to connect to the server. DON'T EVER DO THAT. DON'T YOU EVER EXPOSE YOUR WHOLE DATABASE TO THE INTERNET