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:bariopendatalab.db.DBAccess.java

public void insertDocument(Document doc) {
    MongoDatabase database = client.getDatabase(DBNAME);
    MongoCollection<Document> collection = database.getCollection(COLLNAME);
    collection.insertOne(doc);
}

From source file:bariopendatalab.db.DBAccess.java

public void insertMunicipi(Document doc) {
    MongoDatabase database = client.getDatabase(DBNAME);
    MongoCollection<Document> collection = database.getCollection(COLLMUNICIPI);
    collection.insertOne(doc);
}

From source file:cn.edu.hfut.dmic.webcollector.example.Douban2Crawler.java

License:Open Source License

@Override
public void visit(Page page, CrawlDatums next) {
    System.out.println(page.getUrl());
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    // ?//from  ww w .j  a  v a 2s  . com
    MongoDatabase mongoDatabase = mongoClient.getDatabase("douban_crawler");
    System.out.println("Connect to database successfully");
    MongoCollection<org.bson.Document> collection = mongoDatabase.getCollection("moviereview");
    List<Document> documents = new ArrayList<Document>();
    if (page.matchUrl("https://movie.douban.com/review/\\d*/")) {
        index = index + 1;
        //String title = page.select("div[class=article_title]").first().text();
        //String author = page.select("div[id=blog_userface]").first().text();
        //System.out.println("title:" + title + "\tauthor:" + author);
        //HashMap md = page.getMetaData();
        //String review = page.getMetaData("og:description").toString();
        //Document doc = page.getDoc();
        //System.out.println(doc);
        System.out.println("index:" + index);
        String review = page.select("div.main-bd").text();
        System.out.println("review:" + review);
        String movieName = page.select("div.side-back").text();
        System.out.println("movieName:" + movieName);
        ArrayList rc = page.select("div.comment-item");
        Document document = new Document();
        document.put("moviename", movieName);
        document.put("review", review);
        List<String> subreviews = new ArrayList<String>();
        for (int i = 0; i < rc.size(); i++) {
            //System.out.println(rc.get(i).getClass());
            //((org.jsoup.nodes.Element)rc.get(i)).getAllElements().get(3);
            org.jsoup.nodes.Element element = (org.jsoup.nodes.Element) ((org.jsoup.nodes.Element) ((org.jsoup.nodes.Element) rc
                    .get(i)).childNodes().get(3)).childNode(3);
            //Object nodes = ((org.jsoup.nodes.Element)((org.jsoup.nodes.Element)rc.get(i)).childNodes()).getAllElements().get(3);
            String subReview = element.childNode(0).toString();
            System.out.println("subReview:" + subReview);
            if (subReview != "")
                subreviews.add(subReview);
        }
        document.put("subreviews", subreviews);
        collection.insertOne(document);
        //documents.add(document);
        //collection.insertMany(documents);
    } else if (page.matchUrl("https://movie.douban.com/review/best/\\?start=\\d*")) {
        //Document doc = page.getDoc();
        System.out.println("add seed");
        //this.addSeed(page.getUrl());
    }
}

From source file:cn.edu.hfut.dmic.webcollector.plugin.mongo.MongoDBUtils.java

License:Open Source License

public static void insertIfNotExists(MongoCollection col, CrawlDatum datum) {

    String key = datum.getKey();//from ww  w . j a  va  2  s  .  c o  m
    Document idDoc = new Document("_id", key);
    FindIterable findIte = col.find(idDoc);
    if (findIte.first() == null) {
        Document doc = CrawlDatumFormater.datumToBson(datum);
        col.insertOne(doc);
    }
}

From source file:cn.edu.hfut.dmic.webcollector.plugin.mongo.MongoDBUtils.java

License:Open Source License

public static void insertIfNotExists(MongoCollection col, Document doc) {
    String key = doc.getString("_id");
    Document idDoc = new Document("_id", key);
    FindIterable findIte = col.find(idDoc);
    if (findIte.first() == null) {
        col.insertOne(doc);
    }/*from  w w w .  jav a 2  s.c  o  m*/
}

From source file:com.andersen.backendjaxrsproject.EmployeeServices.java

public String addEmployee(Employee employee) {
    String jsonString = "";
    try {/* w  ww.ja  v a 2  s.c om*/
        ObjectMapper mapper = new ObjectMapper();

        MongoCollection<Document> employeeCollection = employeeDB.getCollection("EmployeeCollection");
        employee.setId(employeeCount + 1);

        jsonString = mapper.writeValueAsString(employee);
        Document doc = Document.parse(jsonString);
        employeeCollection.insertOne(doc);

        //get the document back from the database to verify that it was added
        Bson bson = (Bson) com.mongodb.util.JSON.parse("{'id': " + employee.getId() + "}");

        FindIterable<Document> find = employeeCollection.find();
        FindIterable<Document> filteredQuery = find.filter(bson);
        String newString = "";

        for (Document document : filteredQuery) {
            newString = newString + document.toJson() + "\n";
        }

        return newString;
    } catch (JsonProcessingException ex) {
        Logger.getLogger(EmployeeServices.class.getName()).log(Level.SEVERE, null, ex);
    }
    return jsonString;
}

From source file:com.avbravo.jgmongodesktopexample.Start.java

/**
 * @param args the command line arguments
 *//*from   w  w w  .  j av a2  s.  c  o m*/
public static void main(String[] args) {
    // TODO code application logic here
    try {

        PaisesFacade paisesFacade = new PaisesFacade();
        SedesFacade sedesFacade = new SedesFacade();
        ProvinciasFacade provinciasFacade = new ProvinciasFacade();

        MongoClient client = new MongoClient("localhost", 27017);
        MongoDatabase database = client.getDatabase("fantasy");
        MongoCollection<Document> collection = database.getCollection("paises");

        List<Document> documents = (List<Document>) collection.find().into(new ArrayList<Document>());

        //Document document = new Document("name","Cuba")
        //        .append("poblacion", 7777);
        //
        //       collection.insertOne(document);

        // DBCollection coll = database.getCollection("mycol");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");
        Date startDate = simpleDateFormat.parse(simpleDateFormat.format(new Date()));

        MongoCollection<BasicDBObject> coll = database.getCollection("paises", BasicDBObject.class);
        BasicDBObject doc = new BasicDBObject("title", "OrientDB").append("Fecha", startDate)
                .append("url", "http://www.tutorialspoint.com/mongodb/").append("by", "tutorials point");
        coll.insertOne(doc);

        for (Document document : documents) {
            System.out.println(document);
            System.out.println("Siglas" + document.get("Siglas"));
            System.out.println("Pais" + document.get("Pais"));
        }

        //            
        //            List<Provincias> list = provinciasFacade.findAll();
        //            list.forEach((p) -> {
        //                System.out.println(" "+p.toString());
        //            });

        //                    
        //            Provincias provincias = new Provincias();
        //            provincias.setIdprovincia("6");
        //            Provincias pr = provinciasFacade.findById(provincias);
        //            
        //            if(pr.getIdprovincia() == null){
        //                System.out.println("---> no hay provincia con ese id");
        //            }else{
        //                System.out.println("---> "+pr.toString());
        //            }

        //            Paises paises = new Paises();
        //            paises.setSiglas("pa");
        //            Paises p = paisesFacade.findById(paises);
        //
        //         if(p.getSiglas() == null){
        //                System.out.println("============================");
        //                System.out.println("no hay paises con siglas "+paises.getSiglas());
        //            }else{
        //                System.out.println("paises: "+p.toString());
        //            }
        //            Provincias provincias = new Provincias();
        //            provincias.setIdprovincia("2");
        //            provincias.setProvincia("Cocle");
        //            provincias.setPaises(p);
        //          //  provincias.setHola("Hola");
        //            
        //            if(provinciasFacade.save(provincias)){
        //                System.out.println("guardado");
        //            }else{
        //                System.out.println("no se guardo");
        //            }
        //          
        //            List<Paises> list = paisesFacade.findAll();
        //          Sedes sedes = new Sedes();
        //               sedes.setIdsede("s-2");
        //                sedes.setSede("Sede 2");
        //                sedes.setPaises(list);
        //                sedesFacade.save(sedes);
        //          

    } catch (Exception e) {
        System.out.println("error " + e.getLocalizedMessage());
    }

}

From source file:com.averageloser.mongodemo.Model.BookDBHelper.java

@Override
public boolean insertDocument(MongoCollection<Document> collection, Book object) {
    Document document = getBookDocument(object);

    try {/*ww  w . j  ava2 s  .co m*/
        collection.insertOne(document);

        return true;
    } catch (MongoWriteException | MongoWriteConcernException e) {
        e.printStackTrace();
    }

    return false;
}

From source file:com.bc.fiduceo.db.MongoDbDriver.java

License:Open Source License

@Override
public void insert(SatelliteObservation satelliteObservation) throws SQLException {
    final MongoCollection<Document> observationCollection = database.getCollection(SATELLITE_DATA_COLLECTION);

    final Document document = new Document(DATA_FILE_KEY, satelliteObservation.getDataFilePath().toString());
    document.append(START_TIME_KEY, satelliteObservation.getStartTime());
    document.append(STOP_TIME_KEY, satelliteObservation.getStopTime());
    document.append(NODE_TYPE_KEY, satelliteObservation.getNodeType().toId());

    final Geometry geoBounds = satelliteObservation.getGeoBounds();
    if (geoBounds != null) {
        document.append(GEO_BOUNDS_KEY, convertToGeoJSON(geoBounds));
    }/*from  ww  w .  jav a  2s  .  c o m*/

    // @todo 2 tb/tb does not work correctly when we extend the sensor class, improve here 2016-02-09
    document.append(SENSOR_KEY, new Document("name", satelliteObservation.getSensor().getName()));

    final TimeAxis[] timeAxes = satelliteObservation.getTimeAxes();
    if (timeAxes != null) {
        document.append(TIME_AXES_KEY, convertToDocument(timeAxes));
    }

    document.append(VERSION_KEY, satelliteObservation.getVersion());

    observationCollection.insertOne(document);
}

From source file:com.bdnc.ecommercebdnc.dao.DAODocumentos.java

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