Example usage for com.mongodb.client MongoCollection insertOne

List of usage examples for com.mongodb.client MongoCollection insertOne

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection insertOne.

Prototype

void insertOne(TDocument document);

Source Link

Document

Inserts the provided document.

Usage

From source file:com.u2apple.rt.db.dao.DeviceLogDao.java

public void updateDeviceLog(String productId, String[] vids, Model model) {
    MongoCollection<Document> deviceLog = getDeviceLog();
    Document filter = new Document("product-id", productId);
    Document doc = getDeviceLog().find(filter).first();
    if (doc != null && model != null) {
        List<Document> models = doc.get("models", List.class);
        Document m = convertModel(vids, model);
        if (models != null) {
            models.add(m);/*from  w w w. ja  v a 2 s . co m*/
        } else {
            models = new ArrayList<>();
            models.add(m);
        }
        doc.append("models", models);
        deviceLog.replaceOne(filter, doc);
    } else {
        Device device = new Device();
        device.setProductId(productId);
        doc = toDocument(device);
        List<Document> models = new ArrayList<>();
        models.add(convertModel(vids, model));
        doc.append("models", models);
        deviceLog.insertOne(doc);
    }
}

From source file:com.u2apple.tool.dao.DeviceLogDao.java

public void updateDeviceLog(String productId, String[] vids, Modal model) {
    MongoCollection<Document> deviceLog = getDeviceLog();
    Document filter = new Document("product-id", productId);
    Document doc = getDeviceLog().find(filter).first();
    if (doc != null && model != null) {
        List<Document> models = doc.get("models", List.class);
        Document m = convertModel(vids, model);
        if (models != null) {
            models.add(m);/*from  ww w. ja v  a2s . c om*/
        } else {
            models = new ArrayList<>();
            models.add(m);
        }
        doc.append("models", models);
        deviceLog.replaceOne(filter, doc);
    } else {
        Device device = new Device();
        device.setProductId(productId);
        doc = toDocument(device);
        List<Document> models = new ArrayList<>();
        models.add(convertModel(vids, model));
        doc.append("models", models);
        deviceLog.insertOne(doc);
    }
}

From source file:com.yahoo.ycsb.db3.MongoDbClient.java

License:Open Source License

/**
 * Insert a record in the database. Any field/value pairs in the specified
 * values HashMap will be written into the record with the specified record
 * key.//from w  w  w.ja v  a  2s.c  om
 * 
 * @param table
 *          The name of the table
 * @param key
 *          The record key of the record to insert.
 * @param values
 *          A HashMap of field/value pairs to insert in the record
 * @return Zero on success, a non-zero error code on error. See the {@link DB}
 *         class's description for a discussion of error codes.
 */
@Override
public Status insert(String table, String key, HashMap<String, ByteIterator> values) {
    try {
        MongoCollection<Document> collection = database.getCollection(table);
        Document toInsert = new Document("_id", key);
        for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
            toInsert.put(entry.getKey(), entry.getValue().toArray());
        }

        if (batchSize == 1) {
            if (useUpsert) {
                // this is effectively an insert, but using an upsert instead due
                // to current inability of the framework to clean up after itself
                // between test runs.
                collection.replaceOne(new Document("_id", toInsert.get("_id")), toInsert, UPDATE_WITH_UPSERT);
            } else {
                collection.insertOne(toInsert);
            }
        } else {
            bulkInserts.add(toInsert);
            if (bulkInserts.size() == batchSize) {
                if (useUpsert) {
                    List<UpdateOneModel<Document>> updates = new ArrayList<UpdateOneModel<Document>>(
                            bulkInserts.size());
                    for (Document doc : bulkInserts) {
                        updates.add(new UpdateOneModel<Document>(new Document("_id", doc.get("_id")), doc,
                                UPDATE_WITH_UPSERT));
                    }
                    collection.bulkWrite(updates);
                } else {
                    collection.insertMany(bulkInserts, INSERT_UNORDERED);
                }
                bulkInserts.clear();
            } else {
                return OptionsSupport.BATCHED_OK;
            }
        }
        return Status.OK;
    } catch (Exception e) {
        System.err.println("Exception while trying bulk insert with " + bulkInserts.size());
        e.printStackTrace();
        return Status.ERROR;
    }

}

From source file:Controlador.Controlador.java

private void control() {
    ActionListener actionListener = new ActionListener() {

        public void actionPerformed(ActionEvent actionEvent) {
            //SORTIR
            if (actionEvent.getSource().equals(vista.getjButton6())) {
                System.out.println("Sortint... ADEU!");
                System.exit(0);/*www  .  ja  va2  s.c  o m*/
            }
            //SORTIR DB
            if (actionEvent.getSource().equals(vistaDB.getjButton2())) {
                System.out.println("Sortint... ADEU!");
                System.exit(0);
            }
            //Carrega Coleccions DB
            if (actionEvent.getSource().equals(vistaDB.getjButton1())) {
                carregarColections();
                vistaDB.setVisible(false);
                vista.setVisible(true);
            }
            //Actualitzar document
            if (actionEvent.getSource().equals(vista.getjButton2())) {

                Document d = Document.parse(vista.getjTextArea1().getText());
                System.out.println(d);

                try {
                    MongoCollection col = model.getDatabase()
                            .getCollection(vista.getjList1().getSelectedValue());
                    //UpdateResult resultat = col.updateOne(eq("i", 10), set("i", 110));

                } catch (Exception e) {
                }

            }
            //Inserir documents sencers
            if (actionEvent.getSource().equals(vista.getjButton1())) {

                MongoCollection col = model.getMongoClient().getDatabase(vistaDB.getjList1().getSelectedValue())
                        .getCollection(vista.getjList1().getSelectedValue());

                Document d = Document.parse(vista.getjTextArea1().getText());

                col.insertOne(d);
                carregarDocuments();

            }
            //Borrar documents
            if (actionEvent.getSource().equals(vista.getjButton3())) {

                MongoCollection col = model.getMongoClient().getDatabase(vistaDB.getjList1().getSelectedValue())
                        .getCollection(vista.getjList1().getSelectedValue());

                Document doc = vista.getjList2().getSelectedValue();

                System.out.println(doc.get("_id.timestamp"));

                ObjectId id = (ObjectId) doc.get("_id");
                col.deleteOne(eq("_id", new ObjectId(id.toString())));
                carregarDocuments();
            }
            //Canviar de database
            if (actionEvent.getSource().equals(vista.getjButton7())) {

                vista.setVisible(false);
                vistaDB.setVisible(true);
                carregarKeysCombo();

            }

            if (actionEvent.getSource().equals(vista.getjButton5())) {

                try {
                    MongoCollection col = model.getMongoClient()
                            .getDatabase(vistaDB.getjList1().getSelectedValue())
                            .getCollection(vista.getjList1().getSelectedValue());

                    Document d = vista.getjList2().getSelectedValue();
                    String key = vista.getjComboBox2().getSelectedItem().toString();
                    String value = d.get(key).toString();
                    String newKey = vista.getjTextField1().getText();

                    UpdateResult resultat = col.updateOne(eq(key, value), unset(newKey));

                    carregarDocuments();
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Selecciona un document que modificar");
                }

            }

            if (actionEvent.getSource().equals(vista.getjButton4())) {
                try {
                    MongoCollection col = model.getMongoClient()
                            .getDatabase(vistaDB.getjList1().getSelectedValue())
                            .getCollection(vista.getjList1().getSelectedValue());

                    Document d = vista.getjList2().getSelectedValue();
                    String key = vista.getjComboBox2().getSelectedItem().toString();
                    String value = d.get(key).toString();
                    String newKey = vista.getjTextField1().getText();
                    String newValue = vista.getjTextArea2().getText();

                    if (vista.getjTextField1().getText().equals("")) {
                        UpdateResult resultat = col.updateOne(eq(key, value), set(key, newValue));
                    } else if (!vista.getjTextField1().getText().equals("")) {
                        UpdateResult resultat = col.updateOne(eq(key, value), set(newKey, newValue));
                    }

                    carregarDocuments();
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Selecciona un document que modificar");
                }

            }

        }

    };

    vista.getjButton6().addActionListener(actionListener);
    vistaDB.getjButton2().addActionListener(actionListener);
    vistaDB.getjButton1().addActionListener(actionListener);
    vista.getjButton1().addActionListener(actionListener);
    vista.getjButton7().addActionListener(actionListener);
    vista.getjButton2().addActionListener(actionListener);
    vista.getjButton3().addActionListener(actionListener);
    vista.getjButton5().addActionListener(actionListener);
    vista.getjButton4().addActionListener(actionListener);

    MouseAdapter mouseAdapter = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);

            if (e.getSource().equals(vista.getjList2())) {
                try {
                    filasel = vista.getjList2().getSelectedIndex();
                    if (filasel != -1) {

                        carregarDocumentsGson();
                        carregarKeysCombo();

                    }
                } catch (NumberFormatException ex) {
                }
            }

            if (e.getSource().equals(vista.getjList1())) {

                try {
                    filasel = vista.getjList1().getSelectedIndex();
                    if (filasel != -1) {
                        carregarDocuments();

                    }
                } catch (NumberFormatException ex) {
                }
            }

        }

    };

    vista.getjList2().addMouseListener(mouseAdapter);
    vista.getjList1().addMouseListener(mouseAdapter);

}

From source file:controller.ProductController.java

public void insert(Document doc) {
    MongoCollection<Document> booklist = db.getCollection("product");
    booklist.insertOne(doc);
}

From source file:controller.UserController.java

public void insert(Document doc) {
    MongoCollection<Document> mongoCol = db.getCollection("user");
    mongoCol.insertOne(doc);
}

From source file:dao.DaoDocumentos.java

public boolean salvarCompra(Compra compra) {
    MongoClient client = new MongoClient("localhost", 27017);
    MongoDatabase dataBase = client.getDatabase("bdnc-loja");
    MongoCollection<Document> collection = dataBase.getCollection("vendas");
    collection.insertOne(compra.toDocument());
    client.close();//from w  w w . java 2 s . c  om
    return true;
}

From source file:data.Activity.java

License:Open Source License

public void insertIntoDb(DBManagerMongo manager) throws Exception {
    MongoCollection<Document> coll = manager.getDb().getCollection("activity");

    Document toAdd = new Document("hash", getLocalHash()).append("project_phase_id", getProjectPhaseId())
            .append("project_id", getProjectId()).append("user_login_name", getUserLoginName())
            .append("description", getDescription())
            .append("start_time", Date.from(getStart().toLocalDateTime().atZone(ZoneId.of("UTC")).toInstant()))
            .append("end_time", Date.from(getStop().toLocalDateTime().atZone(ZoneId.of("UTC")).toInstant()))
            .append("comment", getComments()).append("id", manager.getNextSequence("activity"));
    coll.insertOne(toAdd);
    setId(toAdd.getInteger("id"));
}

From source file:data.Project.java

License:Open Source License

public void insertIntoDb(DBManagerMongo manager) throws Exception {
    MongoCollection<Document> coll = manager.getDb().getCollection("project");
    Document toAdd = new Document("hash", getLocalHash()).append("name", getName())
            .append("description", getDescription()).append("id", manager.getNextSequence("project"));
    coll.insertOne(toAdd);
    setId(toAdd.getInteger("id"));
}

From source file:data.ProjectMember.java

License:Open Source License

public void insertIntoDb(DBManagerMongo manager) throws Exception {
    MongoCollection<Document> coll = manager.getDb().getCollection("project_member");
    Document toUpdate = new Document("hash", getLocalHash()).append("user_login_name", getUserLoginName())
            .append("project_id", getProjectId()).append("role", getRole().name());

    coll.insertOne(toUpdate);
}