0

将文档插入 mongodb 集合时出现以下错误

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 的新手。有人可以告诉我为什么会发生此错误以及解决方案是什么?

4

1 回答 1

1

将您要连接的端口号从 28017 更改为 27017。即更改为:

new DBOperations("localhost", 27017);

27017 是 mongod 监听的默认端口。另一方面,28017 是管理 Web 控制台侦听的默认端口。

于 2012-07-05T06:48:32.987 回答