I have been searching but sadly haven't found any resources for using MongoDB with native JS. Every resource I find seems to be NodeJS.
I want to use MongoDB on vanilla JS, so to speak.
CodePudding user response:
See How to connect MongoDB with VanillaJS?
and also link from top comment Connecting MongoDB to the front-end?
You see everyone use node.js because by design, mongodb is meant to communicate with the server only and the server decides how to handle the data. By using nativejs directly (assuming it was possible without using cloud APIs) is a huge security risk. NativeJS advertises itself as "running inside your browser", I wouldn't want my backend code to be exposed to the user.
TL;DR you should use cloud integrations of mongoDB (mongoDB cloud etc.) or use node.js like everyone else.
CodePudding user response:
TL; DR: You can't
MongoDB is not supposed to expose data directly to front-end (client side). NodeJS (in this case; MongoDB can be used with a variety of other server-side engines) is a server-side JavaScript engine that acts as a bridge between the client and the database.
However, you can have a look at MongoDB APIs
The better way, however, would be to have a server between your database and the web interface, that queries or inserts data from your MongoDB database. If your database has to be client-sided though, have a look at firebase.
CodePudding user response:
If you only want to store data on the client, you could simply use LocalStorage API
If not, you either need to render all the data needed in your page content, or query it via js Fetch API
CodePudding user response:
Even the native mongosh
is a NodeJS environment, see MongoDB Shell:
The MongoDB Shell,
mongosh
, is a fully functional JavaScript and Node.js 16.x REPL environment for interacting with MongoDB deployments.
The legacy mongo
shell is also a JavaScript shell, seems to be based on SpiderMonkey, see https://www.mongodb.com/community/forums/t/why-change-v8-to-spidermonkey-in-mongodb-js-engine/99871