Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed mongodb in a java program instead of running a daemon

Tags:

mongodb

Is there a way to embed mongodb in the java application instead of running a daemon.

I have downloaded mongodb jdbc drivers and added them to classpath.

like image 844
Vivek Avatar asked Jul 21 '11 13:07

Vivek


People also ask

How do you connect Java to MongoDB?

To connect to MongoDB on your local instance and default port, you can just omit the URI part of the above, or use a URI like 'mongodb://localhost:27017'. For MongoDB Atlas, you can find the connection string in the cluster console – and make sure you've whitelisted your development IP address for access.

Can you use MongoDB with Java?

Before you start using MongoDB in your Java programs, you need to make sure that you have MongoDB CLIENT and Java set up on the machine. You can check Java tutorial for Java installation on your machine. Now, let us check how to set up MongoDB CLIENT. You need to download the jar mongodb-driver-3.11.


1 Answers

You can not embed mongodb in the java process. It is a separate process.

You can start and stop it when your program starts/stops by spawning the process. This is generally not suggested as mongodb is meant to run as a background server; if you do this is probably best you pick a random port to start it on so it doesn't affect any other mongodb instances. You will also need to make sure you include the correct binaries for the platform your java app is deployed on since it requires more than the JVM.

like image 66
Scott Hernandez Avatar answered Nov 14 '22 16:11

Scott Hernandez