【问题标题】:How to calculate data from MongoDB through Java?如何通过Java计算来自MongoDB的数据?
【发布时间】:2018-01-18 04:02:49
【问题描述】:

我在通过 Java 使用 MongoDb 时遇到问题 我想展示我的作曲程序

DBCollection coll = db.getCollection("datatrain");
DBCursor cursor = coll.find();
Iterator<DBObject> dbo = cursor.iterator();
while (dbo.hasNext()) {
  double column1 = (double) dbo.next().get("column1");
  System.out.println(column1); //result show as I expected
}
System.out.println(column1); //only first document printed and local variable error

有没有什么方法可以在while之外的所有文档中获取数据?我知道 while 中的 column1 是一个列表,我想在 while 之外使用它来成为数组。我试过使用 toArray 但它总是给我错误。在程序中使用 toArray 有什么好的方法吗?

【问题讨论】:

    标签: java arrays mongodb list database


    【解决方案1】:

    使用 mongo-java-driver 3.2+,您可以尝试创建文档列表,然后遍历所有文档以打印您的特定值。这里的示例是 -

    datatrain 集合中的文档可能是类型

    {
        "column1":123124123
    }
    

    其对应的模型应声明为:

    public class DataTrain {
        double column1;
    
        public double getColumn1() {
            return column1;
        }
    
        public void setColumn1(double column1) {
            this.column1 = column1;
        }
    }
    

    然后您可以将以下内容与您的 MongoClient 初始化为:

    MongoDatabase database = mongoClient.getDatabase(databaseName); //get Db
    
    MongoCollection<DataTrain> collection = database.getCollection(collectionName, DataTrain.class); // get Collection
    
    List<DataTrain> dataTrainDocuments = Lists.newArrayList(collection.find()); // find and store the documents
    
    //iterate through and access the column1 attribute of all
    dataTrainDocuments.forEach(dataTrainObject -> System.out.println(dataTrainObject.getColumn1()));
    

    编辑:你应该为你定义的对象模型实现一个代码:

    public class DataTrainCodec implements Codec<DataTrain> {
    
        private DocumentCodec dataTrainCodec;
    
        public DataTrainCodec() {
            this.dataTrainCodec = new DocumentCodec();
        }
    
        @Override
        public void encode(BsonWriter writer, DataTrain dataTrain, EncoderContext encoderContext) {
            org.bson.Document bsonDocument = new org.bson.Document();
            long column1 = dataTrain.getColumn1();
            bsonDocument.put("column1", column1);
            dataTrainCodec.encode(writer, bsonDocument, encoderContext);
        }
    
        @Override
        public Class<DataTrain> getEncoderClass() {
            return DataTrain.class;
        }
    
        @Override
        public DataTrain decode(BsonReader reader, DecoderContext decoderContext) {
            org.bson.Document bsonDocument = documentCodec.decode(reader, decoderContext);
            DataTrain dataTrain = new DataTrain();
            dataTrain.setColumn1(bsonDocument.getLong("column1");
            return document;
        }
    
     }
    

    并将其注册到您的MongoClient 为:

    MongoClientOptions mco = new MongoClientOptions.Builder()
                .codecRegistry(CodecRegistries.fromCodecs(new DataTrainCodec())).build();
    new MongoClient(new ArrayList<ServerAddresses>(), mco);
    

    【讨论】:

    • 我在 Eclipse 上尝试过,但无法解析 Lists.newArrayList 上的列表。我应该启动列表吗?
    • 我添加了 guava.jar 并且没有错误了,但是当我运行时,它得到一个异常,org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class mongodbprj01.InsertDrive$DataTrain. 这是什么意思?
    • 我已经使用了您的建议,但它在 documentCodec.decode (无法解决) 上返回错误。我想念一些lib jar吗?我认为您只是拼写错误,我更改为 DocumentCodec.decode 但它也返回错误(无法从类型 DocumentCodec 对非静态方法 decode(BsonReader, DecoderContext) 进行静态引用)。我的 MongoClient 寄存器中也出现错误 (构造函数 MongoClient(ArrayList, MongoClientOptions) 未定义)
    • @newcomers 你用的是什么版本的mongodb-java-driver?
    【解决方案2】:

    也许有有效的方法,但这解决了我的问题:

    List<Document> matrixes = coll.find().into(new ArrayList<Document>());
    
            ArrayList<Double> myArray = new ArrayList<Double>();
    
            for (Document matrix : matrixes) {
    
                Double column1 = matrix.getDouble("column1");
                myArray.add(column1);
                Double column2 = matrix.getDouble("column2");
                myArray.add(column2);
                Double column3 = matrix.getDouble("column3");
                myArray.add(column3);
                Double column4 = matrix.getDouble("column4");
                myArray.add(column4);
                Double column5 = matrik.getDouble("column5");
                myArray.add(column5);
            }
    
            myArray.toArray();
    

    我只是将它转换为 ArrayList。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      相关资源
      最近更新 更多