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:fr.utbm.repository.MongoDBDao.java

public void subscribeClientToCourseSession(Client cl) {

    MongoClient mongoClient = null;//from  ww  w  . ja v  a2  s.  c  om

    try {
        mongoClient = new MongoClient("localhost", 27017);

        MongoDatabase db;
        db = mongoClient.getDatabase("SCHOOL");

        MongoCollection<Document> collection = db.getCollection("CLIENTS");

        Document doc = new Document();
        doc.put("lastName", cl.getLastName());
        doc.put("firstName", cl.getFirstName());
        doc.put("address", cl.getAddress());
        doc.put("phone", cl.getPhone());
        doc.put("email", cl.getEmail());
        doc.put("courseSessionId", cl.getCourseSessionId().getId());

        collection.insertOne(doc);

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

From source file:fucksocks.server.manager.MongoDBBasedUserManager.java

License:Apache License

@Override
public Void addUser(final String username, final String password) {
    if (mongoDBUtil == null) {
        mongoDBUtil = initMongoDBUtil();
    }//  w w  w .j av a  2 s. c om
    return mongoDBUtil.doJob(new MongoDBCallback<Void>() {
        @Override
        public Void process(MongoCollection<Document> collection) {
            collection.insertOne(
                    new Document().append(USER_USERNAME_KEY, username).append(USER_PASSWORD_KEY, password));
            return null;
        }
    });
}

From source file:geriapp.controller.BootstrapController.java

public static String importJSONFileToDB(String pathToFile, MongoDatabase db, String collectionName) {
    // open file/*  w ww . j  a v  a 2  s .c o  m*/
    FileInputStream fstream = null;
    try {
        fstream = new FileInputStream(pathToFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.out.println("file not exist, exiting");
        return "File Not Found";
    }
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

    // read it line by line
    String strLine;
    MongoCollection newColl = db.getCollection(collectionName);
    try {
        while ((strLine = br.readLine()) != null) {
            // convert line by line to BSON
            Document doc = Document.parse(strLine);
            // insert BSONs to database
            try {
                newColl.insertOne(doc);
            } catch (MongoException e) {
                // duplicate key
                return "Database insertion error";
            }

        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

    return "Success!";

}

From source file:geriapp.dao.ReadingDAO.java

public static void storeReading(Reading reading) {

    MongoClient mongo = new MongoClient("54.254.204.169", 27017);

    MongoDatabase db = mongo.getDatabase("GERI");
    MongoCollection newColl;

    if (reading instanceof MedboxReading) {
        newColl = db.getCollection("Medbox");
        Document medboxReading = new Document("gw_id", ((MedboxReading) reading).getGw_id());
        medboxReading.append("server_timestamp", ((MedboxReading) reading).getServer_timestamp());
        medboxReading.append("sequence", ((MedboxReading) reading).getSequence());
        medboxReading.append("gw_timestamp", ((MedboxReading) reading).getGw_timestamp());
        medboxReading.append("sensor_id", ((MedboxReading) reading).getSensor_id());
        medboxReading.append("reed_val", ((MedboxReading) reading).getReed_val());
        newColl.insertOne(medboxReading);
        //TODO: append reading attributes
    } else if (reading instanceof DoorReading) {
        newColl = db.getCollection("Door");
        Document doorReading = new Document("gw_id", ((DoorReading) reading).getGw_id());
        doorReading.append("server_timestamp", ((DoorReading) reading).getServer_timestamp());
        doorReading.append("sequence", ((DoorReading) reading).getSequence());
        doorReading.append("gw_timestamp", ((DoorReading) reading).getGw_timestamp());
        doorReading.append("sensor_id", ((DoorReading) reading).getSensor_id());
        doorReading.append("reed_val", ((DoorReading) reading).getReed_val());
        newColl.insertOne(doorReading);/*from  w  w w  .j  av  a 2s.  c o  m*/
    }

    mongo.close();
}

From source file:helpers.Canton.java

public ObjectId save() {
    MongoManager mongo = MongoManager.getInstance();

    MongoCollection table = mongo.db.getCollection("canton");

    this.nombre = nombre.toUpperCase();

    Document obj = new Document("idprovincia", this.idProvincia).append("nombre", this.nombre);
    table.insertOne(obj);

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

From source file:helpers.Pais.java

public ObjectId save() {
    MongoManager mongo = MongoManager.getInstance();

    MongoCollection table = mongo.db.getCollection("pais");

    this.nombre = nombre.toUpperCase();

    Document obj = new Document("nombre", this.nombre.toUpperCase());
    table.insertOne(obj);

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

From source file:helpers.Provincia.java

public ObjectId save() {
    MongoManager mongo = MongoManager.getInstance();

    MongoCollection table = mongo.db.getCollection("provincia");

    this.nombre = nombre.toUpperCase();

    Document obj = new Document("idpais", idPais).append("nombre", nombre);
    table.insertOne(obj);

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

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

License:Open Source License

@Override
public boolean appendUserDate(String cid, String uid, String data) {
    MongoCollection<Document> collection = NosqlDB.getCollection(cid);

    collection.insertOne(Document.parse(data));
    return true;//w w w. j ava2s.c  om
}

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

License:Open Source License

@Override
public boolean alterUserDate(String cid, String uid, String data) {
    MongoCollection<Document> collection = NosqlDB.getCollection(cid);
    data = data.replace("\'", "\"");
    Bson json = Document.parse(data);

    collection.deleteMany(Filters.eq("uid", uid));
    collection.insertOne((Document) json);
    //collection.updateMany(Filters.eq("uid", uid), json);
    return true;//w  w w  .j  av  a 2s.c  o m
}

From source file:hrpod.data.MongoDBDao.java

private void insertDocument(String json, String collectionName) {
    MongoCollection<Document> collection = db.getCollection(collectionName);
    collection.insertOne(Document.parse(json));
}