How to calculate data from MongoDB through Java?
我通过Java使用MangGDB有一个问题我想展示我的计件程序
1 2 3 4 5 6 7 8 | 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中的column1是一个列表,我想在while之外使用它作为数组。我试过用ToArray,但总是出错。在程序中使用ToArray有什么好方法吗?
也许有有效的方法,但这解决了我的问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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(); |
我只是把它转换成数组列表。
使用Mango Java驱动程序3.2 +,您可以尝试创建一个文档列表,然后遍历所有来打印您的特定值。这里有个样品-
您在
1 2 3 | { "column1":123124123 } |
其相应型号应声明为:
1 2 3 4 5 6 7 8 9 10 11 | public class DataTrain { double column1; public double getColumn1() { return column1; } public void setColumn1(double column1) { this.column1 = column1; } } |
然后,您可以对初始化为的
1 2 3 4 5 6 7 8 | 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())); |
编辑:您应该为自己定义的对象模型实现一个代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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; } } |
并将此注册为:
1 2 3 | MongoClientOptions mco = new MongoClientOptions.Builder() .codecRegistry(CodecRegistries.fromCodecs(new DataTrainCodec())).build(); new MongoClient(new ArrayList<ServerAddresses>(), mco); |