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:


The pieces of this are as follows:

hostname port database name 50559 corgis

The dbuser and dbpassword are whatever you set them to when you created the database user (they are NOT your 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: for more details.

MongoDB Java Driver v3.3 References

(This section may not apply if using Spring Boot; instead, see

Other references

More on MongoDB: Java