问题
I am new to mongodb and i have the following code
import com.mongodb.*;
import com.mongodb.Block;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.types.ObjectId;
import static java.util.Arrays.asList;
public class getAssets{
public static void main( String args[] ){
Block<Document> printBlock = new Block<Document>() {
@Override
public void apply(final Document document) {
System.out.println(document.toJson());
}
};
MongoClient mongoClient = new MongoClient("localhost",27017);
MongoDatabase database = mongoClient.getDatabase("test");
System.out.println("Connect to database successfully");
MongoCollection<Document> coll = database.getCollection("asset");
BasicDBList statusList = new BasicDBList();
statusList.add("1");
statusList.add("2");
statusList.add("3");
DBObject statusInClause = new BasicDBObject("$in", statusList);
BasicDBList idList = new BasicDBList();
idList.add("123");
DBObject siteIdInClause = new BasicDBObject("$in", idList);
DBObject fields = new BasicDBObject("asset.status", statusInClause);
fields.put("asset.siteid", siteIdInClause);
DBObject unwind = new BasicDBObject("$unwind", "$asset");
DBObject match = new BasicDBObject("$match", fields);
AggregateIterable<Document> aggr = coll.aggregate(asList(unwind, match));
aggr.forEach(printBlock);
mongoClient.close();
}
}
And i am getting the following error while compling
C:\MongoDB\java>javac -cp .;mongo-java-driver-3.4.1.jar getAssets.java
getAssets.java:46: error: no suitable method found for aggregate(List<DBObject>)
AggregateIterable<Document> aggr = coll.aggregate(asList(unwind, match));
^
method MongoCollection.<TResult>aggregate(List<? extends Bson>,Class<TResult>) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)
method MongoCollection.aggregate(List<? extends Bson>) is not applicable
(actual argument List<DBObject> cannot be converted to List<? extends Bson> by method invocation conversion)
where TResult is a type-variable:
TResult extends Object declared in method <TResult>aggregate(List<? extends Bson>,Class<TResult>)
1 error
Note : Using mongo 3.4.1
New Code :
import com.mongodb.*;
import com.mongodb.Block;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.types.ObjectId;
import static java.util.Arrays;
import static java.util.Arrays.asList;
public class getAssets{
public static void main( String args[] ){
Block<Document> printBlock = new Block<Document>() {
@Override
public void apply(final Document document) {
System.out.println(document.toJson());
}
};
MongoClient mongoClient = new MongoClient("localhost",27017);
MongoDatabase database = mongoClient.getDatabase("test");
System.out.println("Connect to database successfully");
MongoCollection<Document> coll = database.getCollection("asset");
Document unwind = new Document("$unwind", "$dp.asset");
Document match = new Document("$match", new Document("$dp.asset.status", new Document("$in", new String[]{"ACTIVE", "LIMITEDUSE", "OPERATING"})).
append("$dp.asset.siteid", new Document("$in", new String[]{"BEDFORD"})));
AggregateIterable<Document> aggr = coll.aggregate(Arrays.asList(unwind, match));
aggr.forEach(printBlock);
mongoClient.close();
}
}
Compiles with no error but runtime error
Exception in thread "main" java.lang.NoSuchMethodError: org.bson.BsonDocument.clone()Lorg/bson/BsonDocument;
at com.mongodb.connection.ClientMetadataHelper.createClientMetadataDocument(ClientMetadataHelper.java:146)
at com.mongodb.connection.ClientMetadataHelper.createClientMetadataDocument(ClientMetadataHelper.java:136)
at com.mongodb.connection.InternalStreamConnectionFactory.<init>(InternalStreamConnectionFactory.java:41)
at com.mongodb.connection.DefaultClusterableServerFactory.create(DefaultClusterableServerFactory.java:68)
at com.mongodb.connection.BaseCluster.createServer(BaseCluster.java:360)
at com.mongodb.connection.SingleServerCluster.<init>(SingleServerCluster.java:54)
at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:114)
at com.mongodb.Mongo.createCluster(Mongo.java:744)
at com.mongodb.Mongo.createCluster(Mongo.java:728)
at com.mongodb.Mongo.<init>(Mongo.java:293)
at com.mongodb.Mongo.<init>(Mongo.java:288)
at com.mongodb.Mongo.<init>(Mongo.java:284)
at com.mongodb.MongoClient.<init>(MongoClient.java:179)
at com.mongodb.MongoClient.<init>(MongoClient.java:156)
at com.mongodb.MongoClient.<init>(MongoClient.java:146)
at getAssets.main(getAssets.java:20)
回答1:
For me , I resolved this error by using mongodb driver 3.6.4 Add this in your dependency(gradle for example)
dependencies {
implementation 'org.mongodb:mongodb-driver:3.6.4'
}
来源:https://stackoverflow.com/questions/41378959/java-code-gives-error-in-mongodb-while-compiling