MongoDB Sample

This sample contains a server definition that includes the MongoDB, JNDI, and Servlet features. The application, mongoDBApp.war, uses the Liberty Extended Profile's ability to configure a MongoDB database to inject a com.mongodb.DB instance into a class to perform a number of insert/find operations. The DB object is injected using a @javax.annotation.Resource annotation using the following code:

public class MongoServlet extends HttpServlet {
	@Resource(name = "mongo/sampledb")
	protected DB db;

This sample assumes that a MongoDB instance is running on localhost:27017 (default mongo settings).

This sample can be installed onto runtime versions and later.

Installation Instructions

Online installation (requires or later):

  1. Install the sample by running the command below from the bin directory of your server installation:
    installUtility install mongoDBSample

Any missing features required by the server will be installed for you.

Offline installation:

  1. Download the mongoDBSample.jar using the download button at the top of the page and place it in the root of your server installation.
  2. Extract the sample into the Liberty installation by running the command below from the root of your server installation:
    java -jar mongoDBSample.jar

Any missing features required by the server will need to be installed separately.

Running The Application

To run the MongoDB application:

  1. Ensure that the MongoDB instance is running on localhost:27017 or update the server.xml file to point to your MongoDB instance see Notes below for more infomation on doing this.
  2. Start the mongoDBSample server by running the command below from the bin directory in the Liberty profile installation:
    server run mongoDBSample
  3. Confirm the application started by looking for the following message in the log:
    CWWKZ0001I: Application mongoDBApp started in XX.XX seconds.
  4. In your browser, enter the URL for the application: http://localhost:9121/mongoDBApp (where port 9121 assumes the httpEndpoint provided in the sample server.xml has not been modified).
  5. The com.mongodb.DB resource was injected if you see the following message:
  6. After HelloMongo! is displayed, a number of elements will also be inserted into the database and the contents of the database will be dumped. See Notes below for example output.
  7. Stop the server using the command 'bin/server stop mongoDBSample'.
  8. Examine the Java source files in the application for more information about how the injection works.


Example output from a successful run :

[count] collection count = 0 from [sampledb]
[inserting] 10
[dump entire db] :
{ "_id" : { "$oid" : "51684c16ecd91696ca5176a1"} , "user" : 0}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a2"} , "user" : 1}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a3"} , "user" : 2}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a4"} , "user" : 3}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a5"} , "user" : 4}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a6"} , "user" : 5}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a7"} , "user" : 6}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a8"} , "user" : 7}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a9"} , "user" : 8}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176aa"} , "user" : 9}

Troubleshooting :

  • If your MongoDB instance is running somewhere other than the default localhost:27017, you can change the location via the mongo configuration element in the server.xml file.
  • If you see HelloMongo!, but there isn't a following '[count] collection count ...', check the server SystemOut.log. If there is not a mongoDB process running at localhost:27017, you should see the following warning :
    '[WARNING ] Server seen down: localhost/ - - message: Connection refused: connect'.
    The solution to this problem is to start a MongoDB process at the default port on the local machine.
  • If you get a 404 response when you access the sample application and you see a SRVE0187E message, you most likely have a problem with your shared library. Ensure that the library element in the server.xml points to a valid Mongo Java Driver jar file.

Sample Structure

  • wlp
    • usr
      • servers
        • mongoDBSample
          • Copyright.txt
          • apps
            • mongoDBApp.war
          • lafiles
            • LA_en
            • LI_en
          • readme.html
          • server.xml
      • shared
        • resources
          • MongoDBSampleLibs
            • mongo-java-driver-2.11.1.jar