java代码在编译时在mongodb中给出错误

时间:2016-12-29 11:37:50

标签: java mongo-java mongo-java-driver

我是mongodb的新手,我有以下代码

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();
    }



}

我在编译时遇到以下错误

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

注意:使用mongo 3.4.1

新守则:

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();
    }



}

编译时没有错误但运行时错误

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)

2 个答案:

答案 0 :(得分:0)

对我来说,我通过使用mongodb驱动程序3.6.4解决了此错误 将此添加到您的依赖项中(例如,gradle)

dependencies {
    implementation 'org.mongodb:mongodb-driver:3.6.4'
}

答案 1 :(得分:0)

我正在使用IntelliJ IDEA来管理我的依赖关系,我注意到我的库中两次列出了MongoDB bson库(一次为org.mongo:bson:3.0.0,也一次为org.mongo:bson:Latest) 。我删除了“最新”条目,错误消失了。