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:org.piotr.apollo.service.FinishedService.java

public Finished addFinishedTest(Finished finished) {

    MongoCollection collection = mongoDb.getCollection(finishedCollection);

    Document toInsert = new Document();

    toInsert.append("wrong", finished.getWrong());
    toInsert.append("correct", finished.getCorrect());
    toInsert.append("percentage", finished.getPercentage());
    toInsert.append("lesson_id", new ObjectId(finished.getLessonId()));

    lessonService.updateLessonTest(finished);
    collection.insertOne(toInsert);

    return finished;
}

From source file:org.piotr.apollo.service.LessonService.java

public Lesson addNewLessonToSection(Lesson lesson) {
    MongoCollection lessonCollection = mongoDB.getCollection(this.lessonCollection);

    Document obj = new Document();

    obj.put("code", lesson.getCode());
    obj.put("section_Id", new ObjectId(lesson.getSectionId()));
    obj.put("name", lesson.getName());
    obj.put("description", lesson.getDescription());
    obj.put("testCounter", 0);
    obj.put("percentageCorrect", 0.0);
    obj.put("grade", 0.0);

    lessonCollection.insertOne(obj);

    return lesson;
}

From source file:org.piotr.apollo.service.QuestionService.java

public ObjectId addQuestion(Question question, ObjectId testId) {
    MongoCollection collection = db.getCollection(questionCollection);

    Document objInsert = new Document();

    objInsert.put("number", question.getNumber());
    objInsert.put("question", question.getQuestion());
    objInsert.put("test_id", testId);

    collection.insertOne(objInsert);

    return (ObjectId) objInsert.get("_id");
}

From source file:org.radarcns.connect.mongodb.MongoWrapper.java

License:Apache License

/**
 * Store a document in MongoDB. If the document has an ID, it will replace existing
 * documents with that ID. If it does not, it will be inserted and MongoDB will assign it with
 * a unique ID./*  w w  w. j a  v  a  2  s. c  o m*/
 *
 * @param topic Kafka topic that the document belongs to
 * @param doc MongoDB document
 * @throws MongoException if the document could not be stored.
 */
public void store(String topic, Document doc) throws MongoException {
    MongoCollection<Document> collection = getCollection(topic);
    Object mongoId = doc.get(MONGO_ID_KEY);
    if (mongoId != null) {
        collection.replaceOne(eq(MONGO_ID_KEY, mongoId), doc, UPDATE_UPSERT);
    } else {
        collection.insertOne(doc);
    }
}

From source file:org.search.system.dao.ImageDao.java

License:Open Source License

public void insertImage(Image image) {
    MongoInstance mongoInstance = databaseManager.getInstance();
    MongoClient mongo = new MongoClient(mongoInstance.getHost(), mongoInstance.getPort());
    try {/*from   w  w  w  . j a  va2s  .  c  o  m*/
        MongoDatabase pages = mongo.getDatabase(DATABASE_NAME);
        Document document = new Document();
        document.put("title", image.getTitle());
        document.put("description", image.getDescription());
        document.put("link", image.getLink());
        document.put("tags", image.getTags());
        document.put("imageHash", image.getImageHash());
        for (String tag : image.getTags()) {
            MongoCollection<Document> collection = pages.getCollection(tag.toLowerCase());
            collection.insertOne(document);
        }
        for (String tag : image.getTitle().split(" ")) {
            MongoCollection<Document> collection = pages.getCollection(tag.toLowerCase());
            collection.insertOne(document);
        }
        MongoCollection<Document> collection = pages.getCollection("imagesHash");
        collection.insertOne(document);
        mongo.close();
    } catch (Exception ex) {
        mongo.close();
        LogUtil.log(ex.toString());
    }
}

From source file:org.search.system.dao.PageDao.java

License:Open Source License

public void insertPage(Page page) {
    MongoInstance mongoInstance = databaseManager.getInstance();
    MongoClient mongo = new MongoClient(mongoInstance.getHost(), mongoInstance.getPort());
    try {/*from   ww w. j  a  v  a  2 s  .com*/
        MongoDatabase pages = mongo.getDatabase(DATABASE_NAME);
        Document document = new Document();
        document.put("title", page.getTitle());
        document.put("description", page.getDescription());
        document.put("rang", 0);
        document.put("link", page.getLink());
        document.put("tags", page.getTags());
        for (String tag : page.getTags()) {
            MongoCollection<Document> collection = pages.getCollection(tag.toLowerCase());
            collection.insertOne(document);
        }
        for (String tag : page.getTitle().split(" ")) {
            MongoCollection<Document> collection = pages.getCollection(tag.toLowerCase());
            collection.insertOne(document);
        }
        mongo.close();
    } catch (Exception ex) {
        mongo.close();
        LogUtil.log(ex.toString());
    }
}

From source file:org.search.system.dao.WordDao.java

License:Open Source License

public void insertPage(Word word) {
    MongoInstance mongoInstance = databaseManager.getInstance();
    MongoClient mongo = new MongoClient(mongoInstance.getHost(), mongoInstance.getPort());
    try {/*from   w  w w.j av a 2 s.co m*/
        MongoDatabase synonyms = mongo.getDatabase("synonyms");
        MongoCollection<Document> collection = synonyms.getCollection("synonyms");
        Document document = new Document();
        document.put("word", word.getWord());
        document.put("synonyms", word.getSynonyms());
        collection.insertOne(document);
        mongo.close();
    } catch (Exception ex) {
        mongo.close();
        LogUtil.log(ex.toString());
    }
}

From source file:org.siphon.jsmongo.MongoExecutor.java

License:Open Source License

public Object insertRow(String table, ScriptObjectMirror row) throws SqlExecutorException, ScriptException {
    MongoCollection<Document> collection = database.getCollection(table);
    Document document = jsObjectToDocument(row);
    collection.insertOne(document);
    return documentToJsObject(document);
}

From source file:org.trade.core.persistence.local.mongo.MongoPersistence.java

License:Apache License

@Override
public void storeBinaryData(byte[] data, String collectionName, String identifier) throws Exception {
    MongoClient client = new MongoClient(new MongoClientURI(this.mongoUrl));
    MongoDatabase db = client.getDatabase(this.dbName);

    MongoCollection<Document> collection = db.getCollection(collectionName);
    Document doc = collection.find(Filters.eq(IDENTIFIER_FIELD, identifier)).limit(1).first();

    if (data == null) {
        // We assume that if the value is set to null, we should delete also the corresponding database entry
        if (doc != null) {
            collection.deleteOne(Filters.eq(IDENTIFIER_FIELD, identifier));
        }//from w  w w  . j a  va  2  s. c  o m
    } else {
        // Check if the document already exists and update it
        if (doc != null) {
            collection.updateOne(Filters.eq(IDENTIFIER_FIELD, identifier),
                    Updates.combine(Updates.set(DATA_FIELD, data), Updates.currentDate("lastModified")));
        } else {
            Document document = new Document(IDENTIFIER_FIELD, identifier).append(DATA_FIELD, data)
                    .append("lastModified", new Date());
            collection.insertOne(document);
        }
    }

    client.close();
}

From source file:Principal.ETL.ETLDAO.Mongo.java

public static void insertimage(Imagen img) {
    Document bson = null;/*  w  ww . j a  v a2 s  .  c  om*/

    try {

        // To connect to mongodb server
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        bson = img.toBson();
        // Now connect to your databases
        MongoDatabase db = mongoClient.getDatabase("test");
        System.out.println("Connect to database successfully");

        MongoCollection coll = db.getCollection("imagenes");
        System.out.println("Collection mycol selected successfully");

        Document i = new Document().append("_id", bson.get("_id"));
        if (!coll.find(i).iterator().hasNext()) {
            coll.insertOne(bson);
        } else {
            coll.findOneAndReplace(i, bson);
        }

        //    FindIterable cursor = coll.find();
        //         MongoCursor x = cursor.iterator();      

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }

}