Example usage for com.mongodb.client MongoCursor next

List of usage examples for com.mongodb.client MongoCursor next

Introduction

In this page you can find the example usage for com.mongodb.client MongoCursor next.

Prototype

@Override
    TResult next();

Source Link

Usage

From source file:fr.lirmm.graphik.graal.keyval.KeyValueStoreMongoDB.java

License:Open Source License

public ArrayList<String> get(PathQuery pathquery) {
    ArrayList<String> arr = new ArrayList<String>();
    String fieldName = pathquery.getPathPredicate().toFieldName();
    String term = pathquery.getTerm().toString();
    MongoCursor<Document> cursor;
    if (pathquery.getTerm().isConstant())
        cursor = currentCollection/* www. ja va 2s .  c om*/
                .find(and(eq(fieldName, term),
                        nor(type(fieldName, BsonType.DOCUMENT), type(fieldName, BsonType.DB_POINTER),
                                type(fieldName, BsonType.UNDEFINED), type(fieldName, BsonType.NULL))))
                .projection(fields(include(fieldName), excludeId())).iterator();
    else
        cursor = currentCollection
                .find(and(exists(fieldName),
                        nor(type(fieldName, BsonType.DOCUMENT), type(fieldName, BsonType.DB_POINTER),
                                type(fieldName, BsonType.UNDEFINED), type(fieldName, BsonType.NULL))))
                .projection(fields(include(fieldName), excludeId())).iterator();
    while (cursor.hasNext()) {
        Document document = (Document) cursor.next();
        arr.add(document.toJson());
    }
    return arr;
}

From source file:fr.lirmm.graphik.graal.keyval.KeyValueStoreMongoDB.java

License:Open Source License

public void formatResult() {
    System.out.println(InterpreterMongoDB.ANSI_YELLOW + "Results : " + InterpreterMongoDB.ANSI_GREEN);
    MongoCursor<Document> resCol = currentCollection.find().projection(excludeId()).iterator();
    Pattern pat = Pattern.compile("\\w+=(?!Document\\{\\{)([\\w+\\ ]+)");
    while (resCol.hasNext()) {
        String doc = resCol.next().toString();
        Matcher mat = pat.matcher(doc);
        while (mat.find()) {
            System.out.println("\t" + mat.group(1));

        }/* w  ww .j  a  v  a  2 s  .c  o m*/

    }
    System.out.println(InterpreterMongoDB.ANSI_RESET);
    //resCol.distinct(arg0, arg1);
    //resCol.updateMany(exists(fieldName),rename(fieldName, "result"));

}

From source file:geriapp.dao.ReadingDAO.java

public static int getPastReadingsCountBetween(String type, Timestamp startTime, Timestamp endTime) {
    MongoClient mongo = new MongoClient("54.254.204.169", 27017);

    MongoDatabase db = mongo.getDatabase("GERI");
    int size = 0;
    MongoCollection<Document> newColl;

    Gson gson = new Gson();

    if (type.equals("medbox")) {
        newColl = db.getCollection("Medbox");
        MongoCursor<Document> iterator = newColl.find().iterator();
        Document latestEntry = null;
        boolean run = true;
        ArrayList<MedboxReading> results = new ArrayList<MedboxReading>();
        while (run) {
            latestEntry = iterator.next();
            if (latestEntry == null) {
                run = false;//www  .  j av  a  2s  .c o  m
                size = 121;
                break;
            }
            String json = latestEntry.toJson();
            MedboxReading reading = gson.fromJson(json, MedboxReading.class);
            String thisTimestamp = reading.getGw_timestamp();

            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date parsedTimestamp = null;
            try {
                parsedTimestamp = df.parse(thisTimestamp);
                //System.out.println(""+parsedTimestamp);
            } catch (ParseException e) {
                e.printStackTrace();
                run = false;
            }
            Timestamp gwTimestamp = new Timestamp(parsedTimestamp.getTime());
            //if (gwTimestamp.after(startTime)) {
            if (gwTimestamp.after(startTime) && gwTimestamp.before(endTime)) {
                results.add(reading);
            }
            if (!iterator.hasNext()) {
                run = false;
            }
        }
        /*
        while (iterator.hasNext()) {
        latestEntry = iterator.next();
        String json = latestEntry.toJson();
        MedboxReading reading = gson.fromJson(json, MedboxReading.class);
        results.add(reading);
        }
        */
        mongo.close();
        size = results.size();
        return size;
    } else if (type.equals("door")) {
        newColl = db.getCollection("Door");
        MongoCursor<Document> iterator = newColl.find().iterator();
        Document latestEntry = null;
        boolean run = true;
        ArrayList<DoorReading> results = new ArrayList<DoorReading>();
        while (run) {
            latestEntry = iterator.next();
            if (latestEntry == null) {
                run = false;
                size = 121;
                break;
            }
            String json = latestEntry.toJson();
            DoorReading reading = gson.fromJson(json, DoorReading.class);
            String thisTimestamp = reading.getGw_timestamp();

            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date parsedTimestamp = null;
            try {
                parsedTimestamp = df.parse(thisTimestamp);
                //System.out.println(""+parsedTimestamp);
            } catch (ParseException e) {
                e.printStackTrace();
                run = false;
            }
            Timestamp gwTimestamp = new Timestamp(parsedTimestamp.getTime());
            //if (gwTimestamp.after(startTime)) {
            if (gwTimestamp.after(startTime) && gwTimestamp.before(endTime)) {
                results.add(reading);
            }
            if (!iterator.hasNext()) {
                run = false;
            }
        }
        /*
        while (iterator.hasNext()) {
        latestEntry = iterator.next();
        String json = latestEntry.toJson();
        MedboxReading reading = gson.fromJson(json, MedboxReading.class);
        results.add(reading);
        }
        */
        mongo.close();
        size = results.size();
        return size;
    }
    return size; //throw Exception??
}

From source file:GUI.Controlador.java

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Buscar")) {
        Document doc = new Document();

        String nombre = vista.nombre.getText();
        System.out.println(nombre);
        if (!nombre.equals("")) {
            doc.append("name", nombre);
        }//w ww.  j  av  a 2  s.c om
        String ext = vista.extension.getSelectedItem().toString();

        if (!ext.equals("-")) {
            doc.append("ext", ext);
        }
        String directory = vista.directorios.getSelectedItem().toString();
        if (!directory.equals("-")) {
            doc.append("meta.directory", directory);
        }

        for (int i = 0; i < vista.tagList.getRowCount(); i++) {

            String activetag = vista.tagList.getValueAt(i, 0).toString();
            doc.append("meta.data.tag", activetag);
        }
        ///String tag = vista.e.getSelectedItem().toString();
        //            if(!tag.equals("-")){
        //                doc.append("meta.data.tag", tag);
        //            }

        FindIterable cur = Consultas.queryNombre(doc);

        MongoCursor x = cur.iterator();
        DefaultListModel<String> listaNombre = new DefaultListModel<>();
        while (x.hasNext()) {
            Document p = (Document) x.next();
            listaNombre.addElement(p.get("_id").toString() + " " + p.get("path").toString() + "/"
                    + p.get("name").toString() + "." + p.get("ext").toString());

        }
        vista.ListaImagenes.setModel(listaNombre);
        vista.numImg.setText(Integer.toString(vista.ListaImagenes.getModel().getSize()));

    }
    if (e.getActionCommand().equals("Add")) {
        Object[] row = { vista.etiquetas.getSelectedItem() };
        vista.TableModel.addRow(row);
    }
    if (e.getActionCommand().equals("Del")) {
        int row = vista.tagList.getSelectedRow();
        vista.TableModel.removeRow(row);
    }
}

From source file:GUI.Controlador.java

private void Extensiones() {

    Document doc = new Document();
    FindIterable cursor = Consultas.queryExtension();
    TreeSet<String> conjunto = new TreeSet<>();

    MongoCursor x = cursor.iterator();

    while (x.hasNext()) {
        int i = 1;
        Document p = (Document) x.next();
        conjunto.add(p.get("ext").toString());
        i++;//from www. j  a  va2 s .c  o m
    }

    for (String s : conjunto) {
        vista.extension.addItem(s);
    }
}

From source file:GUI.Controlador.java

private void Directorios() {

    Document doc = new Document();
    AggregateIterable cursor = Consultas.queryDirectorios();

    MongoCursor x = cursor.iterator();

    while (x.hasNext()) {
        int i = 1;
        Document p = (Document) x.next();
        vista.directorios.addItem(p.get("_id").toString());
        i++;/*  w  w w  .j av a  2 s . co m*/
    }

}

From source file:GUI.Controlador.java

private void Etiquetas() {

    Document doc = new Document();
    AggregateIterable cursor = Consultas.queryTags();

    MongoCursor x = cursor.iterator();

    while (x.hasNext()) {
        int i = 1;
        Document p = (Document) x.next();
        vista.etiquetas.addItem(p.get("_id").toString());
        i++;/*from w ww . j av a2  s .  c om*/
    }

}

From source file:GUI.Controlador.java

@Override
public void mouseClicked(MouseEvent e) {
    int ind = vista.ListaImagenes.getSelectedValue().toString().indexOf(' ');
    ArrayList<String> Atag = new ArrayList<>();
    int id = Integer.parseInt(vista.ListaImagenes.getSelectedValue().toString().substring(0, ind));
    if (vista.tagList.getRowCount() > 0) {
        for (int i = 0; i < vista.tagList.getRowCount(); i++) {
            Atag.add(vista.tagList.getValueAt(i, 0).toString());
            System.out.println(Atag.toString());
        }/*w  ww.j  a v  a 2  s  . c o  m*/
    }
    AggregateIterable cur2 = Consultas.queryMeta(id);

    MongoCursor x2 = cur2.iterator();
    DefaultListModel<String> listaMeta = new DefaultListModel<>();
    while (x2.hasNext()) {
        Document p = (Document) x2.next();
        //listaMeta.addElement(p.get("meta").toString());
        Document w = (Document) p.get("meta");
        Document z = (Document) w.get("data");
        if (!Atag.isEmpty() && (Atag.contains(z.get("tag").toString()))) {
            listaMeta.addElement(w.get("directory").toString() + ": " + z.get("tag").toString() + ": "
                    + z.get("value").toString());
        } else if (Atag.isEmpty()) {
            listaMeta.addElement(w.get("directory").toString() + ": " + z.get("tag").toString() + ": "
                    + z.get("value").toString());
        }
        //listaMeta.addElement(p.get("meta.directory").toString()+": "+p.get("meta.data.tag").toString()+": "+p.get("meta.data.value").toString());
    }
    vista.MetaList.setModel(listaMeta);
}

From source file:henu.dao.impl.CaclDaoImpl.java

License:Open Source License

@Override
public String getUserData(String cid, String uid) {
    MongoCollection<Document> collection = NosqlDB.getCollection(cid);
    String json = "";
    FindIterable<Document> findIterable = collection.find(Filters.eq("uid", uid));
    MongoCursor<Document> mongoCursor = findIterable.iterator();

    while (mongoCursor.hasNext()) {
        json += mongoCursor.next().toJson();
    }/*from ww  w.j  a  va  2  s  .c om*/
    return json;
}

From source file:henu.dao.impl.CaclDaoImpl.java

License:Open Source License

@Override
public List getCaclData(String cid) {
    MongoCollection<Document> collection = NosqlDB.getCollection(cid);

    /**//from w ww. j  a  v  a 2s .c  om
     * 
     * 1. ?FindIterable<Document>
     * 2. ?MongoCursor<Document>
     * 3. ??? 
     *
     */
    List<String> data = new ArrayList();
    FindIterable<Document> findIterable = collection.find();
    MongoCursor<Document> mongoCursor = findIterable.iterator();

    while (mongoCursor.hasNext()) {
        data.add(mongoCursor.next().toJson());
        System.out.println(data);
    }

    return data;
}