MongoDB: Java

Accessing MongoDB via Java

These instructions assume that you have already created a MongoDB instance somewhere.

You might have followed these three steps, for example:

If you did so, you’ll be able to look up the MongoDB URI, something such as:

mongodb://<dbuser>:<dbpassword>@ds050559.mlab.com:50559/corgis

The pieces of this are as follows:

hostname port database name
ds050559.mlab.com 50559 corgis

The dbuser and dbpassword are whatever you set them to when you created the database user (they are NOT your mlab.com username and password.)

With this, you are ready to start coding.

You may find it helpful, if you are using Spring Boot, to start with this code as an example:

The Spring Boot way of using MongoDB is a bit different from the standard way, i.e. using the MongoDB Java Driver; Spring Boot provides some nice “middleware” that hides some of the complexity.

See: https://pconrad-webapps.github.io/topics/mongodb_spring_boot/ for more details.

MongoDB Java Driver v3.3 References

(This section may not apply if using Spring Boot; instead, see https://pconrad-webapps.github.io/topics/mongodb_spring_boot/)

Other references

More on MongoDB: Java