how to remove MongoInternalException?
I am trying to perform CRUd operations in mongodb and scala.. i have written following code.this code is compiling succesfully but giving runtime exception.I am using scala 2.8.can any 1 help me out?
Error while execution:
Exception in thread "main" com.mongodb.MongoInternalException: DBPort.findOne failed at com.mongodb.DBPort.findOne(DBPort.java:153) at com.mongodb.DBPort.runCommand(DBPort.java:159) at com.mongodb.DBTCPConnector.testMaster(DBTCPConnector.java:369) at com.mongodb.Mongo.(Mongo.java:140) at com.mongodb.Mongo.(Mongo.java:124) at com.mongodb.Mongo.(Mongo.java:81) at QuickTour.main(QuickTour.java:16) Caused by: java.io.IOException: couldn't connect to [/127.0.0.1:8005] bc:java.net.ConnectException: Connection refused: connect at com.mongodb.DBPort._open(DBPort.java:205) at com.mongodb.DBPort.go(DBPort.java:85) at com.mongodb.DBPort.findOne(DBPort.java:145) ... 6 more
// crud.scala
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
object crud
{
def main(args:Array[String])
{
while(true)
{
println("")
println("1.Insert ")
println("2. Retriever ")
println("3. Update")
println("4. Delete")
println("5. Exit")
println("")
println("Enter choice : ")
val ch=Console.readInt
ch match
{
case 1=>
insert();
case 2=>
retrieve() ;
case 3=>
update();
case 4=>
delete();
case 5=>
System.exit(0)
}
}
} //main
def insert()
{
println("Enter the rollno")
var roll=Console.readInt
println("Enter the name")
var name=Console.readLine
println("Enter the age")
var age=Console.readInt
println("Enter the city")
var city=Console.readLine
println("Enter the marks")
var marks=Console.readInt
val mongo = new Mongo();
var databaseName = mongo.getDB("mydb")
var collectionName = databaseName.getCollection("student")
val dbObject = new BasicDBObject()
dbObject.put("rollno", "roll")
dbObject.put("name", "name")
dbObject.put("age", "age")
dbObject.put("city", "city")
collectionName.insert(dbObject);
println("Inserted succesfully");
}
def retrieve()
{
println("Enter the rollno")
var r=Console.readInt
val mongo = new Mongo()
开发者_JS百科 var databaseName = mongo.getDB("mydb")
var collectionName = databaseName.getCollection("student")
val query=new BasicDBObject();
val query1=new BasicDBObject();
query.put("marks",new BasicDBObject("$gt","70"));
var cursor = new DBCursor(collectionName,query,query1)
println("The list of students is")
while(cursor.hasNext)
{
println(cursor.next());
}
} //retrieve
def update()
{
println("Enter the rollno of student to be updated")
var no=Console.readInt
val mongo = new Mongo()
var databaseName = mongo.getDB("mydb")
var collectionName = databaseName.getCollection("student")
val query=new BasicDBObject();
val query1=new BasicDBObject();
query.put("rollno",new BasicDBObject("$eq",no));
var cursor = new DBCursor(collectionName,query,query1)
}
def delete()
{
println("Enter the rollno of student to be deleted")
var no=Console.readInt
val mongo = new Mongo()
var databaseName = mongo.getDB("mydb")
var collectionName = databaseName.getCollection("student")
val query=new BasicDBObject();
val query1=new BasicDBObject();
query.put("rollno",new BasicDBObject("$eq",no));
var cursor = new DBCursor(collectionName,query,query1)
while(cursor.hasNext)
{
collectionName.remove(cursor.next())
}
} //delete
}
The error says "couldn't connect to [/127.0.0.1:8005]" you could start with making sure that MongoDB is running, and that it's running on that port. The standard MongoDB port is 27017.
精彩评论