Example usage for com.mongodb BasicDBObject BasicDBObject

List of usage examples for com.mongodb BasicDBObject BasicDBObject

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject BasicDBObject.

Prototype

public BasicDBObject() 

Source Link

Document

Creates an empty object.

Usage

From source file:Modificar.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    //String e = EdadT.getText();
    //int i = Integer.parseInt(EdadT.getText());

    if (Nombre.getText().equals("") || ApPaterno.getText().equals("") || ApMaterno.getText().equals("")
            || EdadT.getText().equals("") || matricula.getText().equals("") || Carrera.getText().equals("")) {
        JOptionPane.showMessageDialog(null, "Faltan campos por llenar");
    } else {//  w  w  w . j  av a  2 s . c  o  m
        //            document.put("Nombre", ""+ NombreT.getText() +"");
        //            document.put("ApPaterno", ""+ ApPaterno.getText() +"");
        //            document.put("ApMaterno", ""+ ApMaterno.getText() +"");
        //            document.put("FechaNac", (String) dia.getSelectedItem() + "-" + mes.getSelectedItem() + "-" + anio.getSelectedItem());
        //            document.put("Edad", Integer.parseInt(EdadT.getText()));
        //            document.put("Sexo", ""+ sexo.getSelectedItem() +"");
        //            document.put("Matricula", ""+ matricula.getText() +"");
        //            document.put("Semestre", Integer.parseInt(""+ semestre.getSelectedItem()));
        //            document.put("Carrera", ""+ Carrera.getText() +"");
        //            collection.insert(document);

        Object[] obj = new Object[] { NombreT.getText(), ApPaterno.getText(), ApMaterno.getText(),
                dia.getSelectedItem() + "-" + mes.getSelectedItem() + "-" + anio.getSelectedItem(),
                Integer.parseInt(EdadT.getText()), sexo.getSelectedItem(), matricula.getText(),
                Integer.parseInt("" + semestre.getSelectedItem()), Carrera.getText() };
        BasicDBObject updateQuery = new BasicDBObject();
        updateQuery.append("$set", createBDBOfromArrays(columnnames, obj));
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("$and", createListBDBOfromArrays(this.columnnames, this.data));
        System.out.println(updateQuery.toString());
        System.out.println(searchQuery.toString());
        System.err.println(collection.update(searchQuery, updateQuery).getError());
        javax.swing.JOptionPane.showMessageDialog(this, "Se ha actualizado exitosamente el alumno");
        PanModificar newFrame = new PanModificar();
        newFrame.setVisible(true);
        this.dispose();
    }

    // TODO add your handling code here:
}

From source file:loc.java

public void loc() {
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB dbBusiness = mongoClient.getDB("business");
    DBCollection collBusiness = dbBusiness.getCollection("business");
    List<DBObject> categoryIdList = new ArrayList<DBObject>();
    DBCursor cursorForBusiness = collBusiness.find();
    while (cursorForBusiness.hasNext()) {
        categoryIdList.add(cursorForBusiness.next());
    }/* ww  w . java  2  s.c o m*/
    for (DBObject s : categoryIdList) {
        System.out.println(s.get("business_id").toString());
        System.out.println(s.get("longitude").toString());
        System.out.println(s.get("latitude").toString());

        BasicDBObject newDocument = new BasicDBObject();
        BasicDBList addFields = new BasicDBList();
        addFields.add(s.get("longitude").toString());
        addFields.add(s.get("latitude").toString());
        newDocument.append("$set", new BasicDBObject().append("loc", addFields));

        BasicDBObject searchQuery = new BasicDBObject().append("business_id", s.get("business_id").toString());

        collBusiness.update(searchQuery, newDocument);
    }

}

From source file:TeacherOne.java

private void GetdataBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GetdataBtnActionPerformed
    // TODO add your handling code here:
    stuFirst = FirstnameField.getText();
    stuMid = MidnameField.getText();//from www .j av  a2  s . co  m
    stuLast = LastnameField.getText();
    grade = Integer.parseInt(GradeField.getText());
    section = Integer.parseInt(SectionField.getText());
    DBCollection coll = db.getCollection("student");
    BasicDBObject andQuery = new BasicDBObject();
    List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
    obj.add(new BasicDBObject("firstname", stuFirst));
    obj.add(new BasicDBObject("mid_name", stuMid));
    obj.add(new BasicDBObject("lastname", stuLast));
    obj.add(new BasicDBObject("grade", grade));
    obj.add(new BasicDBObject("section", section));

    andQuery.put("$and", obj);
    DBCursor cursor = coll.find(andQuery);

    if (cursor.count() != 0) {
        UpdateBtn.setVisible(true);
        MidField.setVisible(true);
        FinalField.setVisible(true);
        jLabel6.setVisible(true);
        jLabel7.setVisible(true);
        jLabel8.setVisible(true);

        BasicDBList marks = (BasicDBList) cursor.next().get("marks");
        for (int j = 0; j < marks.size(); ++j) {
            BasicDBObject mark = (BasicDBObject) marks.get(j);

            if (mark.getString("teacher").equals(firstname)) {

                if (mark.get("mid") == null) {

                    MidField.setText("-1");
                    FinalField.setText("-1");

                } else if (mark.get("mid") != null && mark.get("final") == null) {

                    MidField.setText(mark.getString("mid"));
                    FinalField.setText("-1");
                } else {

                    MidField.setText(mark.getString("mid"));
                    FinalField.setText(mark.getString("final"));

                }

            }
        }
    } else {
        JOptionPane.showMessageDialog(null, "wrong data,try again!");
        FirstnameField.setText("");
        MidnameField.setText("");
        LastnameField.setText("");
        GradeField.setText("");
        SectionField.setText("");
    }
}

From source file:TeacherOne.java

private void UpdateBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UpdateBtnActionPerformed
    // TODO add your handling code here:
    DBCollection coll = db.getCollection("student");
    BasicDBObject andQuery = new BasicDBObject();
    List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
    obj.add(new BasicDBObject("firstname", stuFirst));
    obj.add(new BasicDBObject("mid_name", stuMid));
    obj.add(new BasicDBObject("lastname", stuLast));
    obj.add(new BasicDBObject("grade", grade));
    obj.add(new BasicDBObject("section", section));
    obj.add(new BasicDBObject("marks.teacher", firstname));
    andQuery.put("$and", obj);
    BasicDBObject data = new BasicDBObject();
    if (MidField.getText().equals("-1") && FinalField.getText().equals("-1")) {
        JOptionPane.showMessageDialog(null, "No data was updated!");
    } else if (!MidField.getText().equals("-1") && FinalField.getText().equals("-1")) {
        data.put("marks.$.mid", MidField.getText());
        JOptionPane.showMessageDialog(null, "data was updated .");
    } else {//  w ww  .  j  a  v a 2  s  .c o m
        data.put("marks.$.mid", MidField.getText());
        data.put("marks.$.final", FinalField.getText());
        JOptionPane.showMessageDialog(null, "data was updated .");

    }
    BasicDBObject command = new BasicDBObject();
    command.put("$set", data);
    coll.update(andQuery, command);

}

From source file:MongoCredentialsExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    String server = args[0];//from  w w w .  j  a  va2  s.c o m
    String user = args[1];
    String password = args[2];
    String databaseName = args[3];

    System.out.println("server: " + server);
    System.out.println("user: " + user);
    System.out.println("database: " + databaseName);

    System.out.println();

    MongoClient mongoClient = new MongoClient(new ServerAddress(server),
            Arrays.asList(MongoCredential.createMongoCRCredential(user, "test", password.toCharArray())),
            new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
    DB testDB = mongoClient.getDB(databaseName);

    System.out.println("Count: " + testDB.getCollection("test").count());

    System.out.println("Insert result: " + testDB.getCollection("test").insert(new BasicDBObject()));

}

From source file:Vehicle_type_check.java

private void bike_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bike_buttonActionPerformed
    // TODO add your handling code here:

    try {//from  ww w.  j ava2s  . c  o  m
        int count_bike;
        System.out.println("Hello_End");
        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection = db.getCollection("vehicle_count");

        BasicDBObject query = new BasicDBObject();
        BasicDBObject field = new BasicDBObject();
        field.put("wheeler_2", 1);
        DBCursor cursor = collection.find(query, field);

        BasicDBObject obj = (BasicDBObject) cursor.next();
        count_bike = obj.getInt("wheeler_2");
        System.out.println(count_bike);

        if (count_bike < 50) {

            BasicDBObject searchUpdate = new BasicDBObject();
            searchUpdate.append("wheeler_2", count_bike);

            BasicDBObject UpdateObject = new BasicDBObject();
            UpdateObject.append("$set", new BasicDBObject("wheeler_2", count_bike + 1));

            collection.update(searchUpdate, UpdateObject);

            setVisible(false);
            new Veehicle_entry().setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "No Available Space For Parking 2 Wheelers");
        }

    } catch (Exception e) {

    }
}

From source file:Vehicle_type_check.java

private void other_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_other_buttonActionPerformed
    // TODO add your handling code here:

    try {/*  ww  w . ja v  a 2 s.c  o m*/
        int count_other;

        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection = db.getCollection("vehicle_count");

        BasicDBObject query = new BasicDBObject();
        BasicDBObject field = new BasicDBObject();
        field.put("other", 1);
        DBCursor cursor = collection.find(query, field);

        BasicDBObject obj = (BasicDBObject) cursor.next();
        count_other = obj.getInt("other");
        System.out.println(count_other);

        if (count_other < 50) {

            BasicDBObject searchUpdate = new BasicDBObject();
            searchUpdate.append("other", count_other);

            BasicDBObject UpdateObject = new BasicDBObject();
            UpdateObject.append("$set", new BasicDBObject("other", count_other + 1));

            collection.update(searchUpdate, UpdateObject);

            setVisible(false);
            new Veehicle_entry().setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "No Available Space For Parking Trucks");
        }

    } catch (Exception e) {

    }

}

From source file:Vehicle_type_check.java

private void car_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_car_buttonActionPerformed
    // TODO add your handling code here:
    try {/* w w w.jav  a  2 s .c  om*/
        int count_car;

        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection = db.getCollection("vehicle_count");

        BasicDBObject query = new BasicDBObject();
        BasicDBObject field = new BasicDBObject();
        field.put("wheeler_4", 1);
        DBCursor cursor = collection.find(query, field);

        BasicDBObject obj = (BasicDBObject) cursor.next();
        count_car = obj.getInt("wheeler_4");
        System.out.println(count_car);

        if (count_car < 50) {

            BasicDBObject searchUpdate = new BasicDBObject();
            searchUpdate.append("wheeler_4", count_car);

            BasicDBObject UpdateObject = new BasicDBObject();
            UpdateObject.append("$set", new BasicDBObject("wheeler_4", count_car + 1));

            collection.update(searchUpdate, UpdateObject);

            setVisible(false);
            new Veehicle_entry().setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "No Available Space For Parking 2 Wheelers");
        }

    } catch (Exception e) {

    }

}

From source file:Ventana.java

private void doitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doitActionPerformed

    String casilla_id = jTextField1.getText();

    switch (funcion) {
    case INSERTAR:
        if (casilla_id.isEmpty())
            JOptionPane.showMessageDialog(null, "No hay datos en la casilla ID");
        else {//w w  w . java2 s . com
            BasicDBObject document = new BasicDBObject();

            for (int i = 0;; i++) {
                if (fields[i].isVisible() == true)
                    document.put(fields[i].getText(), texts[i].getText());
                else
                    break;
            }

            coleccion.insert(document);

            setSize(345, 51);
            JOptionPane.showMessageDialog(null, "Documento con ID: " + casilla_id + " ha sido INSERTADO");
        }
        ;
        break;

    case ELIMINAR:
        if (casilla_id.isEmpty())
            JOptionPane.showMessageDialog(null, "No hay datos en la casilla ID");
        else {
            BasicDBObject document = new BasicDBObject();

            document.put(jLabel1.getText(), casilla_id);
            coleccion.remove(document);

            setSize(345, 51);
            JOptionPane.showMessageDialog(null, "Documento(s) con ID: " + casilla_id + " ha sido ELIMINADO");
            setSize(345, 51);
            consultar();
        }

        ;
        break;

    case ACTUALIZAR:
        if (casilla_id.isEmpty())
            JOptionPane.showMessageDialog(null, "No hay datos en la casilla ID");
        else {
            BasicDBObject document_OLD = new BasicDBObject();
            BasicDBObject document_NEW = new BasicDBObject();

            document_OLD.put(jLabel1.getText(), casilla_id);
            document_NEW.put(jLabel1.getText(), casilla_id);

            for (int i = 1;; i++) {
                if (fields[i].isVisible() == true)
                    document_NEW.put(fields[i].getText(), texts[i].getText());
                else
                    break;
            }

            coleccion.update(document_OLD, document_NEW);

            setSize(345, 51);
            JOptionPane.showMessageDialog(null, "Documento con ID: " + casilla_id + " ha sido ACTUALIZADO");
            setSize(345, 51);
            consultar();
        }

        ;
        break;

    }

}

From source file:NegotiatedAuthenticationProtocolExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException, InterruptedException {
    String server = args[0];//from  ww  w .j av  a  2 s .  c  o  m
    String user = args[1];
    String pwd = args[2];
    String db = args[3];

    MongoCredential credentials = new MongoCredential(user, pwd.toCharArray(),
            MongoAuthenticationProtocol.NEGOTIATE, db);

    MongoClient mongoClient = new MongoClient(new ServerAddress(server), Arrays.asList(credentials),
            new MongoClientOptions.Builder().build());

    DB testDB = mongoClient.getDB(db);
    testDB.getCollection("test").insert(new BasicDBObject());
    System.out.println("Count: " + testDB.getCollection("test").count());
}