Example usage for com.mongodb DBCollection insert

List of usage examples for com.mongodb DBCollection insert

Introduction

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

Prototype

public WriteResult insert(final List<? extends DBObject> documents) 

Source Link

Document

Insert documents into a collection.

Usage

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

License:Open Source License

@Override
public void visit(Page page, CrawlDatums next) {
    if (page.matchUrl("http://pf.maoyan.com/rankings/america.*")) {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        // ?//from w w  w.j a v a2 s  .  c om
        // DBCollection dbCollection = mongoClient.getDB("maoyan_crawler").getCollection("rankings_am"); 
        DB db = mongoClient.getDB("maoyan_crawler");
        // ?????
        Set<String> colls = db.getCollectionNames();
        for (String s : colls) {
            // Collection(?"")
            if (s.equals("rankings_am")) {
                db.getCollection(s).drop();
            }
        }
        DBCollection dbCollection = db.getCollection("rankings_am");
        String title = page.select("span[id=year-box]").text();
        Elements table = page.select("table[id=na-list]");
        Elements data_set = table.select("tr");
        List amList = new ArrayList();
        for (Element id : data_set) {
            Elements tds = id.select("td");
            for (Element td : tds) {
                amList.add(td.text());
            }
            String en_name = tds.select("p[class=first-line]").text();
            String cn_name = tds.select("p[class=second-line]").text();
            if (amList.size() > 0) {
                System.out.println(amList);
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title).append("rank", amList.get(0)).append("mov_cnname", cn_name)
                        .append("mov_enname", en_name).append("toweek_rev", amList.get(2))
                        .append("total_rev", amList.get(3)).append("val_week", amList.get(4));
                amList.removeAll(amList);
                dbCollection.insert(dbObject);
            }
        }
        mongoClient.close();
    } else if (page.matchUrl("http://pf.maoyan.com/rankings/day.*")) {
        MongoClient mongoClient2 = new MongoClient("localhost", 27017);
        // ?
        DB db = mongoClient2.getDB("maoyan_crawler");
        // ?????
        Set<String> colls = db.getCollectionNames();
        for (String s : colls) {
            // Collection(?"")
            if (s.equals("rankings_day")) {
                db.getCollection(s).drop();
            }
        }
        DBCollection dbCollection2 = db.getCollection("rankings_day");
        String title = page.select("span[id=year-box]").text();
        String update_time = page.select("span[id=update-time]").text();
        title = title + update_time;
        System.out.println(title);
        Elements data_set = page.select("tr");
        List dayList = new ArrayList();
        for (Element id : data_set) {
            Elements tds = id.select("td");
            for (Element td : tds) {
                dayList.add(td.text());
            }
            if (dayList.size() > 0) {
                System.out.println(dayList);
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title).append("rank", dayList.get(0))
                        .append("mov_name", dayList.get(1)).append("today_rev", dayList.get(2))
                        .append("date", dayList.get(3)).append("val_week", dayList.get(4));
                dayList.removeAll(dayList);
                dbCollection2.insert(dbObject);
            }
        }
        mongoClient2.close();
    } else if (page.matchUrl("http://pf.maoyan.com/rankings/market.*")) {
        MongoClient mongoClient3 = new MongoClient("localhost", 27017);
        // ?
        DB db = mongoClient3.getDB("maoyan_crawler");
        // ?????
        Set<String> colls = db.getCollectionNames();
        for (String s : colls) {
            // Collection(?"")
            if (s.equals("rankings_market")) {
                db.getCollection(s).drop();
            }
        }
        DBCollection dbCollection3 = db.getCollection("rankings_market");
        String title = page.select("span[id=year-box]").text();
        String update_time = page.select("span[id=update-time]").text();
        title = title + update_time;
        System.out.println(title);
        Elements data_set = page.select("tr");
        List dayList = new ArrayList();
        for (Element id : data_set) {
            Elements tds = id.select("td");
            for (Element td : tds) {
                dayList.add(td.text());
            }
            if (dayList.size() > 0) {
                System.out.println(dayList);
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title).append("rank", dayList.get(0)).append("date", dayList.get(1))
                        .append("today_rev", dayList.get(2)).append("total_sessions", dayList.get(3))
                        .append("total_visit_count", dayList.get(4));
                dayList.removeAll(dayList);
                dbCollection3.insert(dbObject);
            }
        }
        mongoClient3.close();
    } else if (page.matchUrl("http://pf.maoyan.com/rankings/year.*")) {
        MongoClient mongoClient4 = new MongoClient("localhost", 27017);
        // ?
        DB db = mongoClient4.getDB("maoyan_crawler");
        // ?????
        Set<String> colls = db.getCollectionNames();
        for (String s : colls) {
            // Collection(?"")
            if (s.equals("rankings_year")) {
                db.getCollection(s).drop();
            }
        }
        DBCollection dbCollection4 = db.getCollection("rankings_year");
        String title = page.select("span[id=year-box]").text();
        String update_time = page.select("span[id=update-time]").text();
        title = title + update_time;
        System.out.println(title);
        Elements table = page.select("div[id=ranks-list]");
        //             System.out.println(table);
        Elements data_set = table.select("ul[class=row]");
        //             System.out.println(data_set);
        List dayList = new ArrayList();
        for (Element id : data_set) {
            Elements lis = id.select("li");
            for (Element li : lis) {
                dayList.add(li.text());
            }
            String cn_name = lis.select("p[class=first-line]").text();
            String release_date = lis.select("p[class=second-line]").text();
            if (dayList.size() > 0) {
                System.out.println(dayList);
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title).append("rank", dayList.get(0)).append("name", cn_name)
                        .append("release date", release_date).append("year_rev", dayList.get(2))
                        .append("avg_price", dayList.get(3)).append("avg_visit_count", dayList.get(4));
                dayList.removeAll(dayList);
                dbCollection4.insert(dbObject);
            }
        }
        mongoClient4.close();
    } else if (page.matchUrl("http://pf.maoyan.com/")) {
        MongoClient mongoClient5 = new MongoClient("localhost", 27017);
        // ?
        DB db = mongoClient5.getDB("maoyan_crawler");
        // ?????
        Set<String> colls = db.getCollectionNames();
        for (String s : colls) {
            // Collection(?"")
            if (s.equals("main_page")) {
                db.getCollection(s).drop();
            }
        }
        DBCollection dbCollection5 = db.getCollection("main_page");
        String title = page.select("span[id=dayStr]").text();
        String box_type = page.select("span[id=box-type]").text();
        String ticket_count = page.select("span[id=ticket_count]").text();
        box_type = box_type + ticket_count;
        System.out.println(title + "\n" + box_type);
        Elements table = page.select("div[id=ticket_tbody]");
        //System.out.println(table);
        Elements data_set = table.select("ul");
        //System.out.println(data_set);
        List dayList = new ArrayList();
        for (Element id : data_set) {
            Elements lis = id.select("li");
            for (Element li : lis) {
                dayList.add(li.text());
            }
            //           System.out.println(lis);
            String cn_name = lis.select("b").first().text();
            String comment = lis.select("em").text();
            //         System.out.println(cn_name+ "\n" +comment);
            if (dayList.size() > 0) {
                System.out.println(dayList);
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title).append("box_type", box_type).append("name", cn_name)
                        .append("comment", comment).append("realtime_rev", dayList.get(1))
                        .append("rev_percent", dayList.get(2)).append("schedule_percent", dayList.get(3))
                        .append("total_rev", dayList.get(4));
                dayList.removeAll(dayList);
                dbCollection5.insert(dbObject);
            }
        }
        mongoClient5.close();
    }
}

From source file:co.edu.uniandes.csw.Arquidalgos.usuario.persistence._UsuarioPersistence.java

License:MIT License

public UsuarioDTO createUsuario(UsuarioDTO user) {

    //      UsuarioEntity entity=UsuarioConverter.persistenceDTO2Entity(usuario);
    //      entityManager.persist(entity);
    //      return UsuarioConverter.entity2PersistenceDTO(entity);

    DBCollection coll = null;
    BasicDBObject doc = null;/*from w  w w  .j av a2  s  .  c om*/

    UsuarioDTO usuario = (UsuarioDTO) user;
    coll = db.getCollection("UsuarioDTO");
    doc = new BasicDBObject();
    doc.append("name", usuario.getName());
    doc.append("contrasena", usuario.getContrasena());
    doc.append("email", usuario.getEmail());
    doc.append("facebookId", usuario.getFacebookId());

    coll.insert(doc);
    return user;
}

From source file:com.aankam.servlet.MongoCrudServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//  w w w  . j av  a  2 s .  c  o  m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    MongoClientURI uri = new MongoClientURI("mongodb://Username:Pasword@ds061721.mongolab.com:61721/cs612");
    MongoClient client = new MongoClient(uri);
    DB db = client.getDB(uri.getDatabase());

    response.setContentType("text/html;charset=UTF-8");

    String serialNo = request.getParameter("serialNo");
    String name = request.getParameter("name");
    String emailId = request.getParameter("emailId");
    String operation = request.getParameter("operation");

    BasicDBObject customer;
    BasicDBObject updateQuery;
    BasicDBObject deleteQuery;
    BasicDBObject searchQuery;
    DBCollection customersCollection;
    customersCollection = db.getCollection("Customers");
    DBObject searchResult = null;
    boolean flag = false;
    DBCursor cursor;
    switch (operation) {
    case "create":
        customer = new BasicDBObject();
        customer.put("serialNo", serialNo);
        customer.put("name", name);
        customer.put("emailId", emailId);
        customersCollection.insert(customer);
        break;
    case "update":
        updateQuery = new BasicDBObject();
        updateQuery.append("serialNo", serialNo);
        cursor = customersCollection.find(updateQuery);
        customer = new BasicDBObject();
        customer.put("serialNo", serialNo);
        customer.put("name", name);
        customer.put("emailId", emailId);
        if (cursor.hasNext()) {
            customersCollection.update(updateQuery, customer);
            flag = true;
        }

        break;
    case "delete":
        deleteQuery = new BasicDBObject();
        deleteQuery.append("serialNo", serialNo);
        customersCollection.remove(deleteQuery);
        break;
    case "search":
        searchQuery = new BasicDBObject();
        searchQuery.append("serialNo", serialNo);
        cursor = customersCollection.find(searchQuery);
        while (cursor.hasNext()) {
            searchResult = cursor.next();
        }
        break;
    default:
        break;

    }
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet MongoCrudServlet</title>");
        out.println("</head>");
        out.println("<body>");

        switch (operation) {
        case "create":
            out.println("<h3>Customer was successfully created.</h3>");
            break;
        case "update":
            if (flag == true) {
                out.println("<h3>Customer was successfully updated.</h3>");
            } else {
                out.println("<h3>Customer was not found to update.</h3>");
            }

            break;
        case "delete":
            out.println("<h3>Customer was successfully deleted.</h3>");
            break;
        case "search":
            if (searchResult != null) {
                out.println("<h3>The following customer was found:</h3>");
                out.println("<h4>Serial No :" + searchResult.get("serialNo") + "</h4>");
                out.println("<h4>Name :" + searchResult.get("name") + "</h4>");
                out.println("<h4>Email Id :" + searchResult.get("emailId") + "</h4>");
            } else {
                out.println("<h3>Customer not found.</h3>");
            }
            break;
        default:
            break;
        }
        out.println("<a href=\"index.html\">Back to Main Form</a>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java

License:Open Source License

public Resource getRootFolder(String username) {
    DBCollection files = mongo.getDataBase().getCollection("files");

    DBObject filter = new BasicDBObject();
    filter.put("user", username);
    filter.put("root", true);

    DBObject root = files.findOne(filter);

    if (root == null) {
        BasicDBObject newRoot = new BasicDBObject();
        newRoot.append("type", Resource.ResourceType.FOLDER.toString());
        newRoot.append("name", username);
        newRoot.append("root", true);
        newRoot.append("user", username);
        newRoot.append("creationDate", new Date());
        newRoot.append("modificationDate", new Date());
        files.insert(newRoot);
        root = newRoot;//  w  w  w.  j a  va 2  s. c  om
    }

    Resource res = buildResource(root);

    return res;
}

From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java

License:Open Source License

public Resource mkcol(String username, String resource)
        throws ResourceAlreadyExistsException, ResourceNotFoundException {
    String[] path = resource.split("/");
    Resource parent = getResourceAt(getRootFolder(username), Arrays.copyOfRange(path, 0, path.length - 2));

    if (getChild(parent, path[path.length - 1]) != null) {
        throw new ResourceAlreadyExistsException();
    }//from   w w  w .  j ava  2s .  c  om

    DBCollection col = mongo.getDataBase().getCollection("files");

    DBObject obj = new BasicDBObject();
    obj.put("type", Resource.ResourceType.FOLDER.toString());
    obj.put("user", username);
    obj.put("name", path[path.length - 1]);
    obj.put("creationDate", new Date());
    obj.put("modificationDate", new Date());
    obj.put("parent", parent.getId());

    col.insert(obj);

    col.update(new BasicDBObject("_id", parent.getId()),
            new BasicDBObject("$set", new BasicDBObject("modificationDate", new Date())));

    return buildResource(obj);
}

From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java

License:Open Source License

public void put(final String username, String resource, InputStream data, long length, String contentType,
        final char[] password) throws ResourceNotFoundException, IOException {

    final String[] path = resource.split("/");
    Resource parent = getResourceAt(getRootFolder(username), Arrays.copyOfRange(path, 0, path.length - 2));

    DBCollection files = mongo.getDataBase().getCollection("files");
    DBCollection contents = mongo.getDataBase().getCollection("contents");

    ContentDetector contentDetector = null;
    if (contentType == null) {
        PipedInputStream pipeIn = new PipedInputStream();
        PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
        TeeInputStream tee = new TeeInputStream(data, pipeOut, true);
        contentDetector = new ContentDetector(path[path.length - 1], pipeIn);
        contentDetector.start();/*  ww  w.  j  av a  2s  .  c om*/
        data = tee;
    }

    final File dataFile = createDataFile(data, username, password);

    if (contentDetector != null) {
        try {
            contentDetector.join();
            contentType = contentDetector.getContentType();
        } catch (InterruptedException ex) {
            Logger.getLogger(MongoFileService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    Resource child = getChild(parent, path[path.length - 1]);
    if (child != null) {
        DBObject filter = new BasicDBObject();
        filter.put("_id", child.getId());
        DBObject update = new BasicDBObject("modificationDate", new Date());
        update.put("contentLength", length);
        update.put("contentType", contentType);
        files.update(filter, new BasicDBObject("$set", update));

        contents.update(new BasicDBObject("resource", child.getId()),
                new BasicDBObject("$set", new BasicDBObject("file", dataFile.getAbsolutePath())));
    } else {
        DBObject childObj = new BasicDBObject();
        ObjectId objId = new ObjectId();
        childObj.put("_id", objId);
        childObj.put("user", username);
        childObj.put("name", path[path.length - 1]);
        childObj.put("parent", parent.getId());
        childObj.put("type", Resource.ResourceType.FILE.toString());
        childObj.put("creationDate", new Date());
        childObj.put("modificationDate", new Date());
        childObj.put("contentType", contentType);
        childObj.put("contentLength", length);

        files.insert(childObj);

        DBObject content = new BasicDBObject();
        content.put("resource", objId);
        content.put("file", dataFile.getAbsolutePath());

        contents.insert(content);

        files.update(new BasicDBObject("_id", parent.getId()),
                new BasicDBObject("$set", new BasicDBObject("modificationDate", new Date())));

        child = buildResource(childObj);
    }

    final String fContentType = contentType;
    final Resource fChild = child;
    new Thread() {
        public void run() {
            try {
                Map<String, String> metadata = extractionService.extractContent(path[path.length - 1],
                        readFile(dataFile, username, password), fContentType);

                metadata.put("name", path[path.length - 1]);

                indexService.remove(username, new String(password), fChild.getId().toString());
                indexService.index(username, new String(password), fChild.getId().toString(), metadata);
            } catch (Exception ex) {
                Logger.getLogger(MongoFileService.class.getName()).log(Level.SEVERE,
                        "Index failed for " + path[path.length - 1], ex);
            }
        }
    }.start();
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@SuppressWarnings("unchecked")
protected void storeObject(Object object, String collectionName) throws IOException {
    String json = constructDbId(object);
    // use ObjectMapper in order to represent expiresIn as integer not as double - 100 instead of 100.00
    Map<String, Object> result = new ObjectMapper().readValue(json, Map.class);
    BasicDBObject dbObject = new BasicDBObject(result);

    DBCollection coll = db.getCollection(collectionName);
    coll.insert(dbObject);
    log.debug("dbObject:", result);
}

From source file:com.app.mongoDao.MongoBookDao.java

@Override
public String insertBook(Book book) {
    String status = "";
    try {/*ww w .  ja  v a  2  s  . c  o  m*/
        DBCollection data = DatabaseConfig.configure();
        BasicDBObject insertbook = new BasicDBObject();
        insertbook.put("bookname", book.getBookname());
        insertbook.put("author", book.getAuthor());
        data.insert(insertbook);
        status = "Book created successfully";
    } catch (Exception e) {
        status = "Book  not created successfully";
        e.printStackTrace();

    }
    return status;
}

From source file:com.aw.util.MongoDbUtil.java

public static WriteResult saveCollection(String dbName, String collectionName, DBObject document) {
    DB dataBase = getDB(dbName);//from  ww  w . j av a2 s . co m
    if (dataBase != null) {
        DBCollection collection = dataBase.getCollection(collectionName);
        return collection.insert(document);
    } else {
        return null;
    }
}

From source file:com.aw.util.MongoDbUtil.java

public static WriteResult saveCollection(String collectionName, DBObject document) {
    DB dataBase = getDB(defaultDBName);// ww w  .  j a v a2  s  .c  o  m
    if (dataBase != null) {
        DBCollection collection = dataBase.getCollection(collectionName);
        return collection.insert(document);
    } else {
        return null;
    }
}