我正在尝试创建一个简单的连接,并使用java(eclipse 4.7.1a)将文档插入本地MongoDB实例。 MongoDB已经上线了通过" Compass"运行并且能够进行操作同样。
package com;
import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
public class connectToDB {
public static void main( String args[] ) {
try{
// To connect to mongodb server
MongoClient mongoClient = new MongoClient( "localhost" , 27017
);
// Now connect to your databases
DB db = mongoClient.getDB("test");
System.out.println("Connect to database successfully");
// if collection doesn't exists, MongoDB will create it for you
DBCollection coll = db.getCollection("testCollection");
System.out.println("Collection coll selected successfully");
// insert
List<Integer> books = Arrays.asList(27464, 747854);
DBObject documents = new BasicDBObject("_id", "jo")
.append("name", "Jo Bloggs")
.append("address", new BasicDBObject("street", "123 Fake St")
.append("city", "Faketon")
.append("state", "MA")
.append("zip", 12345))
.append("books", books);
coll.insert(documents);
System.out.println("Document added to Collection coll successfully");
// update
BasicDBObject query = new BasicDBObject();
query.put("name", "Xo");
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("name", "Jo Bloggs-updated");
BasicDBObject updateObj = new BasicDBObject();
updateObj.put("$set", newDocument);
coll.update(query, updateObj);
System.out.println("Document updated to Collection coll successfully");
// find - search
BasicDBObject searchQuery1 = new BasicDBObject();
searchQuery1.put("name", "mkyong");
DBCursor cursor = coll.find(searchQuery1);
System.out.println("Selection from Collection coll done successfully");
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
// delete
BasicDBObject searchQuery2 = new BasicDBObject();
searchQuery2.put("name", "Xo");
coll.remove(searchQuery2);
System.out.println("Document deleted from Collection mycol2 successfully");
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
但是当执行时,在eclipse中以跟随错误结束。
Nov 01, 2017 2:19:31 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE,
requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms',
maxWaitQueueSize=500}
Exception in thread "main" java.lang.NoSuchMethodError:
org.bson.BsonDocument.clone()Lorg/bson/BsonDocument;
at com.mongodb.connection.ClientMetadataHelper.
createClientMetadataDocument(ClientMetadataHelper.java:159)
at com.mongodb.connection.ClientMetadataHelper.
createClientMetadataDocument(ClientMetadataHelper.java:149)
at com.mongodb.connection.InternalStreamConnectionFactory.<init>
(InternalStreamConnectionFactory.java:37)
at com.mongodb.connection.DefaultClusterableServerFactory.
create(DefaultClusterableServerFactory.java:58)
at com.mongodb.connection.BaseCluster.createServer(BaseCluster.java:359)
at com.mongodb.connection.SingleServerCluster.<init>
(SingleServerCluster.java:52)
at com.mongodb.connection.DefaultClusterFactory.
createCluster(DefaultClusterFactory.java:147)
at com.mongodb.Mongo.createCluster(Mongo.java:726)
at com.mongodb.Mongo.createCluster(Mongo.java:720)
at com.mongodb.Mongo.<init>(Mongo.java:290)
at com.mongodb.Mongo.<init>(Mongo.java:285)
at com.mongodb.Mongo.<init>(Mongo.java:281)
at com.mongodb.MongoClient.<init>(MongoClient.java:186)
at com.mongodb.MongoClient.<init>(MongoClient.java:163)
at com.mongodb.MongoClient.<init>(MongoClient.java:153)
at com.connectToDB.main(connectToDB.java:25)
MongoDB版本:v3.4.9 使用以下jar文件。 bson 3.0.4.jar&amp;蒙戈-java的驾驶员3.5.0.jar
有没有人可以帮我解决为什么会出现这个错误?
答案 0 :(得分:1)
在使用MongoDB代码运行简单的hello world程序之前,您的代码工作正常,
public class connectToDB {
public static void main( String args[] ) {
System.out.println("Hello world");
}
}
如果有效,则将MongoDB代码放在main中并再次运行。如果它不起作用,那么首先需要学习“如何使用Eclipse IDE?”在你跳进数据库之前。
答案 1 :(得分:0)
我从引用的代码库中删除了“ bson-3.0.2.jar”,并且可以正常工作。 如错误消息所述,
NoSuchMethodError: org.bson.BsonDocument.clone()Lorg / bson / BsonDocument;
似乎bson jar和mongodb java驱动程序之间的某些方法依赖项已崩溃。