将 MongoDB 文档的 ArrayList 加载到 Java class

Load an ArrayList of MongoDB documents into a Java class

所以我想将这个 ArrayList lineaCompra 加载到我的 Java class 中。他们有相同的属性,但我找不到办法。

这里是Mongo中的文档:

{
    "_id" : ObjectId("5a15b007862b6815ac785e54"),
    "IDVenta" : 1,
    "IDCliente" : "12365456",
    "fechaCompra" : "22/11/2017",
    "lineaCompra" : [
            {
                    "idProducto" : "libro1",
                    "cantidad" : 2,
                    "precio" : 12
            },
            {
                    "idProducto" : "libro2",
                    "cantidad" : 2,
                    "precio" : 12
            }
    ]
}

我的Javaclass

public class Compra {

    private String IDCliente; 
    private Date fechaCompra;
    private ArrayList<LineaCompra> Compras = new ArrayList<LineaCompra>();

}

LineaCompraclass(我加载的那个)

public class LineaCompra {

    private String titulo; //IDPRoducto
    private int cantidad;
    private float precio;

}

我尝试加载它的代码

BasicDBObject query = new BasicDBObject();
query.put("IDVenta", doc2.get("IDVenta"));  
//doc2 is all the document loaded,Should I put something else on 
//the query?
FindIterable<Document> cursor = collection.find(query); 
while (cursor.iterator().hasNext()) {
    DBObject theObj = ((Iterator<DBObject>) cursor).next(); 
    //Getting a exception here
    BasicDBList lineasList = (BasicDBList)theObj.get("lineaCompra");
    for (int j = 0; j < lineasList.size(); i++) {
        BasicDBObject lineasObj = (BasicDBObject)lineasList.get(j);
        aux2=new LineaCompra();
        aux2.setTitulo(lineasObj.getString("idProducto"));
        Double x=(Double) lineasObj.get("precio");
        aux2.setPrecio(x.floatValue());
        aux2.setCantidad(lineasObj.getInt("cantidad"));
        lineas.add(aux2);
    }               
}
aux.setCompras((ArrayList<LineaCompra>) lineas);
compras.add(aux);

希望有人知道怎么做。我不知道。我做了一些测试,我加载了除该数组之外的所有内容。我尝试将结果转换为 ArrayList<LineaCompra> 但没有成功。

鉴于您问题中的文档,以下代码...

FindIterable<Document> cursor = collection.find(Filters.eq("IDVenta", 1));

for (Document document : cursor) {
    // the lineaCompra attribute is an array of sub documents
    // so read it as a List and then iterate over that List
    // with each element in the List being a Document
    List<Document> lineaCompra = document.get("lineaCompra", List.class);
    for (Document d : lineaCompra) {
        System.out.println("idProducto: " + d.get("idProducto"));
        System.out.println("cantidad: " + d.get("cantidad"));
        System.out.println("precio: " + d.get("precio"));
        System.out.println("----");
    }
}

... 将打印:

idProducto: libro1
cantidad: 2
precio: 12
----
idProducto: libro2
cantidad: 2
precio: 12
----

这显示了如何

  • 通过 IDVenta
  • 查找
  • 提取lineaCompra子文档数组的内容

要为 List<Document> lineaCompra 中的每个文档创建并填充 LineaCompra 的实例,您可以这样做:

for (Document d : lineaCompra) {
    LineaCompra() aux2 = new LineaCompra();
    aux2.setTitulo(d.getString("idProducto"));
    aux2.setPrecio(d.getDouble("precio").floatValue());
    aux2.setCantidad(lineasObj.getInteger("cantidad"));
    lineas.add(aux2);
}