Use mongodb directly from React

Hi
Can i connect to monogodb directly from react without creating nodejs server
For example
Can i use npm install mongodb
In react frontend folder then import mongodb client in react component and perform CRUD operations directly

Hi @Ali_Aboubkr,

Welcome to the MongoDB Community!

From what I understand, you cannot connect to MongoDB directly from “React” without creating a backend server. React is a front-end framework that is used to build user interfaces. It does not have the ability to connect to databases or perform CRUD operations on its own.

To connect to MongoDB from React, you will need to create a server (say Node.js server) that will act as an intermediary between React and MongoDB.

However, you can consider Next.js as a React framework that includes a built-in server that can be used to connect to MongoDB. This means that you don’t need to create a separate Node.js server for your application. You can refer to this tutorial - How to Integrate MongoDB Into Your Next.js App | MongoDB to learn more.

Hope the above helps!

Regards,
Kushagra

1 Like