2012-07-03 61 views
0

MongoDB中集插入文檔中插入文檔到MongoDB的集合時,我收到以下錯誤錯誤使用Java

Jul 3, 2012 10:27:31 PM com.mongodb.DBTCPConnector fetchMaxBsonObjectSize 
WARNING: Exception determining maxBSONObjectSize 
java.io.EOFException 
    at org.bson.io.Bits.readFully(Bits.java:48) 
    at org.bson.io.Bits.readFully(Bits.java:33) 
    at org.bson.io.Bits.readFully(Bits.java:28) 
    at com.mongodb.Response.<init>(Response.java:40) 
    at com.mongodb.DBPort.go(DBPort.java:127) 
    at com.mongodb.DBPort.go(DBPort.java:92) 
    at com.mongodb.DBPort.findOne(DBPort.java:145) 
    at com.mongodb.DBPort.runCommand(DBPort.java:156) 
    at com.mongodb.DBTCPConnector.fetchMaxBsonObjectSize(DBTCPConnector.java:467) 
    at com.mongodb.Mongo.getMaxBsonObjectSize(Mongo.java:574) 
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:241) 
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:211) 
    at com.mongodb.DBCollection.insert(DBCollection.java:57) 
    at com.mongodb.DBCollection.insert(DBCollection.java:102) 
    at DBOperations.addDocument(DBOperations.java:54) 
    at MongoBook.main(MongoBook.java:28) 
Jul 3, 2012 10:27:39 PM com.mongodb.DBTCPConnector fetchMaxBsonObjectSize 
WARNING: Exception determining maxBSONObjectSize 
java.io.EOFException 
    at org.bson.io.Bits.readFully(Bits.java:48) 
    at org.bson.io.Bits.readFully(Bits.java:33) 
    at org.bson.io.Bits.readFully(Bits.java:28) 
    at com.mongodb.Response.<init>(Response.java:40) 
    at com.mongodb.DBPort.go(DBPort.java:127) 
    at com.mongodb.DBPort.go(DBPort.java:92) 
    at com.mongodb.DBPort.findOne(DBPort.java:145) 
    at com.mongodb.DBPort.runCommand(DBPort.java:156) 
    at com.mongodb.DBTCPConnector.fetchMaxBsonObjectSize(DBTCPConnector.java:467) 
    at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:446) 
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:145) 
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:137) 
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:261) 
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:211) 
    at com.mongodb.DBCollection.insert(DBCollection.java:57) 
    at com.mongodb.DBCollection.insert(DBCollection.java:102) 
    at DBOperations.addDocument(DBOperations.java:54) 
    at MongoBook.main(MongoBook.java:28) 

我的程序如下:

MongoBook類

import com.mongodb.BasicDBObject; 
public class MongoBook 
{ 
    public static void main(String []str) 
    { 
     DBOperations dbops = null; 

     try 
     { 
      dbops = new DBOperations("localhost", 28017); 
      BasicDBObject d1 = new BasicDBObject(); 
      d1.put("Name", "Malhar Vora"); 
      d1.put("Address", "224, Bhatiya Colony"); 
      d1.put("Phone", "9429007086"); 


      dbops.addDocument(d1); 
     } 
     catch(Exception e) 
     { 
      System.out.println("Error occured"); 
     } 

    } 
} 

DBOperations類

import java.net.UnknownHostException; 
import java.util.ArrayList; 

import com.mongodb.Mongo; 
import com.mongodb.DB; 
import com.mongodb.DBCollection; 
import com.mongodb.BasicDBObject; 
import com.mongodb.DBObject; 
import com.mongodb.DBCursor; 

class DBOperations 
{ 
    private String hostUrl; // To store host url of Database Server 
    private int port; // To store port no of Database Server 
    private Mongo mongo = null; 
    private DB db = null; 
    private DBCollection collContacts = null; 

    public DBOperations(String host,int port) throws UnknownHostException 
    { 
     this.hostUrl = host; 
     this.port = port; 
     mongo = new Mongo(host, port); 
     db = mongo.getDB("ContactDB"); 
     collContacts = db.getCollection("Contacts"); 
    } 

    // Method to get host url no for database server 
    public String getHostUrl() { 
     return hostUrl; 
    } 

    // Method to set host url no for database server 
    public void setHostUrl(String hosturl) { 
     this.hostUrl = hosturl; 
    } 

    // Method to get port no for database server 
    public int getPort() { 
     return port; 
    } 

    //Method to set port no for database server 
    public void setPort(int port) { 
     this.port = port; 
    }  

    // Method to add document to collection 
    public void addDocument(BasicDBObject doc) 
    { 
     if (doc!=null) 
     { 
      this.collContacts.insert(doc); 

     } 
    } 

    //Method to retrieve all documents with specific criteria 
    public ArrayList<BasicDBObject> getDocuments(BasicDBObject dbfind) 
    { 
     ArrayList<BasicDBObject> docList = new ArrayList<BasicDBObject>(); 
     DBCursor cur = this.collContacts.find(dbfind); 

     while(cur.hasNext()) 
     { 
      docList.add((BasicDBObject) cur.next()); 
     } 
     return docList; 
    } 

} 

我是MongoDB的新手。 有人可以告訴我爲什麼會發生此錯誤,解決方案是什麼?

+0

http://stackoverflow.com/questions/6499268/mongodb-connection-refused可能的重複 –

回答

1

更改您從28017連接到端口號27017也就是說,更改爲:

new DBOperations("localhost", 27017); 

27017是mongod的監聽的默認端口。另一方面,28017是管理Web控制檯偵聽的默認端口。