You cannot embed mongodb in a java process. This is a separate process.
You can start and stop it when your program starts / stops, creating a process. This is usually not suggested since mongodb is intended to run as a background server; if you do this, it is probably best to choose a random port to start it so that it does not affect other mongodb instances. You also need to make sure that you include the correct binaries for the platform on which the Java application is deployed, since this requires more than the JVM.
source share