Question

I've written a simple Java server. The basic idea is that, upon clicking a button, a servlet will load some data into MongoDB.

However, whenever I try to actually load something into MongoDB (in my case, saving a collection), I get the following error:

com.mongodb.MongoException$Network: can't call something : /127.0.0.1:27017/
com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:295)
com.mongodb.DBTCPConnector.call(DBTCPConnector.java:257)
com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
com.mongodb.DB.command(DB.java:274)
com.mongodb.DB.command(DB.java:256)
com.mongodb.DB.command(DB.java:313)
com.mongodb.DB.command(DB.java:211)
com.mongodb.DB.createCollection(DB.java:170)
servlets.ImportData.doPost(ImportData.java:60)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Does anyone know what the issue here might be? When I searched this problem I found people guessing at everything from port conflicts to stale connections to incorrect installation of MongoDB, so it would be fantastic to get some clarity on this one.

If it helps to see the source code, I'd be more than happy to share it.

Thanks in advance!

Was it helpful?

Solution

Turns out my instance of mongod wasn't running at the time I was executing the commands.

Make sure to run "mongod" in your console at all times you're using the server.

The second error I encountered was failing to specify the host and port of the MongoClient, in the following way:

MongoClient mongo = new MongoClient();

Instead you can do:

MongoClient mongo = new MongoClient("localhost", 27017);

You can stick that into your servlet context and pass it around as needed to other servlets, and while I have declared MongoClient mongo with the new statement here, I would recommend keeping it as a class variable of the ContextListener.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top