How does one go use MongoDB with native JS? - javascript

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.

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.

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.

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

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

Related

Fetching Data From MySQL as Json in ReactJs

The bounty expires in 7 days. Answers to this question are eligible for a +50 reputation bounty.
Om Nigam wants to draw more attention to this question.
How do i fetch data from my MySql database as json and then use it in react js...? actually i'm new to React and MySql so i just don't know much about it.. Please help
React is not allowed to connect to MySql directly. We need NodeJs to start a back-end server which supporting HTTP API.
You can watch this video, and try to create your demo.
React Node.js MySQL CRUD Tutorial for Beginners
I agree with YinPeng.Wei's idea for setting up a backend server to retrieve data from database. The majority programming language for backend development have packages or libraries to connect with Database.
A similar question was asked years ago about How to connect to SQL Server database from JavaScript in the browser?. The answer is you could do that but it is high discouraged.
The general idea is to create a simple backend server which responds to your React frontend requests. Backend retrieves data from MySQL via sql query, then serialize searched data into JSON format and gives back to your frontend.
Not sure which type of programming language would you choose for backend development. I would do either one of Python, C#, Java for a quick demo.
Typically, you are using React to connect to another app (backend) via an API e.g. REST, THAT backend is the one who connect to the database and passes the data to your React app.
Your react app doesn't even know there is a database, all he knows is the backend that's feeding him. without him he's just a pretty looking dead app.
now from your backend, you have two ways, using languages built-in driver to connect to your database directly (hand writing SQL statements), Or you may use an ORM, just google "js orm" and you will find many.
As a start, you need to learn more about creating a simple REST API with whatever language you choose, and then simply use fetch("http://example.com/whatever").then(res=> JSON.parse(res)).then(res=> console.log(res))
the code above should show you whatever you see on the screen when you actually visit the URL inside fetch() from the browser, just text.
REST itself is just a way to use HTTP that people like.
the browser itself is just another client (like your React app), if he can do it so does your app.
first, you have to solve the console error of key of react, give the key wherever you

Store and edit data using ReactJS

I am building an application using ReactJS. I am trying to find out how to store data and to edit it. I tried to store it on my computer with 'fs, 'browserify-fs' but it didn't work.
Should I use express, or is there any other alternatives ?
If you are using React you are operating in the browser. Your option for storage is in local storage. This is explained here.
Examples of code are:
// setter
localStorage.setItem('myData', data);
// getter
localStorage.getItem('myData');
// remove
localStorage.removeItem('myData');
// remove all
localStorage.clear();
Note this is stored in the browser and can be easily cleared. You are going to realize that you need a back end solution. This is a server you can send requests to which has an API (a place you send requests to) which executes some form of operation (normally CRUD - Create Read Update Delete via a REST endpoint or GRAPHQL) to serve you back the data you are requesting from a database (MySQL, Postgres, MongoDB). This is a whole different discussion.
To store an array in local storage you will need to make it a string via JSON.stringify. An example would be:
localStorage.setItem("array", JSON.stringify(array));
In developer tools in Chrome you can go to Application -> Storage -> Local Storage and see what is saved. Here is an example:
If you want to share the data along multiple clients you should use server-side solution or if you just want to save the data for a client only you could use client-side solution provided by #diesel.
Create your own web-server
You need to create web server and a database to store your data. Database is used to store data. You could use: MySQL, PostgreSQL, SQLite3, MongoDB, ... You also need to create web service to make secure database calls.
To create web server you could use Express.js to write your web server easily.
Headless Content Management Systems (abbr: CMS)
If you don't want to spent time on creating your own web-server you could install a headless CMS to read/write your data using api endpoints provided by CMSs. Here's list of headless CMS softwares: headlesscms.org. I tried strapi which has lots of features you might need.
Here's some strapi features:
Open-source
Model builder
Extensible (plugin support)
Content editor (eg: to edit articles)
and many more
Firebase
If you don't want to spend your time on installing CMS software to your server and maintaining it regularly you could use Database service provided by Google Firebase. It is also feature rich too. Here's some features supported by Firebase.
NoSQL Database (to store your data)
Authentication (to authenticate users)
Storage (to store files)
Functions (to write serverless functions)
Machine Learning
and many more

Phonegap/cordova connect directly to MS SQL database (not through any Web)

I have a task to write an Bar-code reader. Which will connect directly to an MS SQL Server to find an product.
Basically, I can write a ASP.net website and write Javascript code to call it from Cordova (Android) app. Or write an native code and call it from Cordova.
But my task it "Directly connect" to Ms SQL server by using Javascript code. Does any way to do this ?
Connecting directly to the database is not a very good idea. Instead, your database should be exposed by an API.
In your case, your best bet is to use the built in ASP.NET WebAPI. You can find more information about ASP.NET WebAPI here.
To call the WebAPI endpoints, you'll want to use HTTP, or HTTPS calls. jQuery has quite a nice set of functions you can use.
You want to ensure that each layer of your application is separated. If you start putting SQL queries inside your mobile app, then this will spell disaster when it comes to any kind of debugging, or even refactoring.

Acessing MongoDB on the Client-Side using JavaScript

I know this is completely unsafe to do because the user would have control over the credentials. But in application I'm building I don't really care it's completely insecure.
Is there a way to open a connection on a MongoDb using client-side JavaScript ?
The JavaScript driver on Mongo's website is for use on NodeJS only.
Thanks,
Fred_
Take a look at the mongodb http interfaces list. Start a mongodb, start the sever, request/modify a data from that server using ajax.

Building a node.js chat

I'm currently building a web chat with node.js for the backend. It uses web sockets for the communication between server and client. I haven't worked with node before and I always used PHP/MySQL and Ajax to store and retrieve data. The chat can be compared to MSN etc. with user accounts and contact lists.
I was looking for some tips and frameworks, which could help me with developing a website which heavily relies on JavaScript and which does all its communication via node.js.
What would you recommend to store the data? Mongo db (mongoose? mongo-db native?), SQLite? An easy solution would be appreciated.
Would you send everything via the websocket and would you establish a new connection for every conversation?
Is there a way to get around PHP completely?
Would you recommend the usage Backbone.js or Ember.js?
Thanks for you help!
What would you recommend to store the data? Mongo db (mongoose?
mongo-db native?), SQLite? An easy solution would be appreciated.
I would suggest you to use redis, because it is insanely fast.
> Example of benchmark result The test was done with 50 simultaneous
> clients performing 100000 requests. The value SET and GET is a 256
> bytes string. The Linux box is running Linux 2.6, it's Xeon X3320 2.5
> GHz. Text executed using the loopback interface (127.0.0.1). Results:
> about 110000 SETs per second, about 81000 GETs per second.
As client I would use node_redis
Would you send everything via the websocket and would you establish a
new connection for every conversation?
websockets are not yet support by every browser(pointing especially to Internet Explorer ;)). I think you should use socket.io which supports multiple transports so that it will work in every major browser.
Is there a way to get around PHP completely?
You could get around PHP completely. Use for example express as your web framework instead of PHP
Would you recommend the usage Backbone.js or Ember.js?
Also need to have a better look at both of them.
Use mongo-db or couchDB to store the data.
Establishing a new connection is better than sending everything through websockets and parsing them.
Yes. Use node.js on the server side.
You can try either one of them. Both have their pros & cons.

Categories