Example usage for com.mongodb.client MongoDatabase getCollection

List of usage examples for com.mongodb.client MongoDatabase getCollection

Introduction

In this page you can find the example usage for com.mongodb.client MongoDatabase getCollection.

Prototype

MongoCollection<Document> getCollection(String collectionName);

Source Link

Document

Gets a collection.

Usage

From source file:eu.vre4eic.evre.telegram.commands.RemoveAuthCommand.java

License:Apache License

@Override
public void execute(AbsSender absSender, User user, Chat chat, String[] arguments) {

    String userName = user.getFirstName() + " " + user.getLastName();
    StringBuilder messageBuilder = new StringBuilder();
    if (arguments.length != 2) {
        messageBuilder.append("Hi ").append(userName).append("\n");
        messageBuilder.append("please use: /remove username pwd");
    } else {/*from  www.  j av  a2s  . c o m*/
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase db = mongoClient.getDatabase("evre");
        MongoCollection<Document> collection = db.getCollection("eVREUserProfile");
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("userId", arguments[0]);
        FindIterable<Document> itcursor = collection.find(searchQuery);
        //FindIterable<Document> itcursor=collection.find();
        MongoCursor<Document> cursor = itcursor.iterator();
        if (cursor.hasNext()) {
            //System.out.println("################## "+cursor.next());
            Document userCan = cursor.next();
            String pwd = userCan.getString("password");
            if (pwd.equals(arguments[1])) {

                String aId = userCan.getString("authId");
                if (!aId.equals("0")) {
                    // we don't check if the chat.getId() is the same,
                    //because a user can remove this from another Telegram ID,
                    // need to check this

                    userCan.replace("authId", "0");
                    BasicDBObject updateObj = new BasicDBObject();
                    updateObj.put("$set", userCan);
                    //check this!!!
                    collection.updateOne(searchQuery, updateObj);

                    messageBuilder.append("Done ").append(userName).append(", \n");
                    messageBuilder.append(
                            "this Telegram account is no longer an e-VRE Authenticator for " + arguments[0]);
                } else {//the user with the provided credentials has no authenticator defined
                    messageBuilder.append("Hi ").append(userName).append(",\n");
                    messageBuilder.append("something went wrong, please contact the administrator!");
                }

            } else {//error credentials wrong
                messageBuilder.append("Hi ").append(userName).append("\n");
                messageBuilder.append("credentials not valid!");
            }

        } else {//error credentials wrong
            messageBuilder.append("Hi ").append(userName).append(",\n");
            messageBuilder.append("credentials not valid!");
        }
        mongoClient.close();
    }

    SendMessage answer = new SendMessage();
    answer.setChatId(chat.getId().toString());
    answer.setText(messageBuilder.toString());

    try {
        absSender.sendMessage(answer);
    } catch (TelegramApiException e) {
        BotLogger.error(LOGTAG, e);
    }
}

From source file:example.Cap1BackupCode.java

public static void main(String[] args) throws IOException {

    String line, prev = "";

    Example example = new Example();
    String[] exclusions = { "100550310 - Food Technology", "100551710 - Oil and Paints Technology",
            "100552410 - Paper and Pulp Technology", "100552710 - Petro Chemical Engineering" };
    //  File file=new File("/home/sachin/Downloads/2014ENGG_Cutoff_CAP1.txt");
    PdfReader reader = new PdfReader("/home/sachin/Downloads/2014ENGG_Cutoff_CAP1.pdf");
    //  System.out.println("This PDF has " + reader.getNumberOfPages() + " pages.");

    for (int i = 1; i < reader.getNumberOfPages(); i += 1) {
        String page = PdfTextExtractor.getTextFromPage(reader, i);
        InputStream is = new ByteArrayInputStream(page.getBytes());
        // read it with BufferedReader
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        int count = 1;
        while ((line = br.readLine()) != null) {
            if (count == 7) {

                //  System.out.println("substring is"+line.substring(6));
                //   System.out.println("Prev college is"+example.getPrevCollege());
                if (!line.substring(6).trim().equals(example.getPrevCollege())) {
                    // System.out.println("College Name" + line);
                    if (example.getPrevCollege() != "") {
                        example.addObject(example.getSinglecollegedata());
                    }//from w  w  w.jav  a2  s  .c o m
                    example.setPrevCollege(line.substring(6).trim());
                    CollegeDataObject object = new CollegeDataObject();
                    object.setCollegeName(line.substring(6).trim());
                    int temp;
                    if ((temp = line.substring(6).trim().split(",").length) > 1) {
                        object.setCity(line.substring(6).trim().split(",")[temp - 1].trim());
                        object.setCollegeName(line.substring(6).trim()
                                .replace(line.substring(6).trim().split(",")[temp - 1].trim(), "").trim()
                                .replace(",", "").trim());
                    }
                    example.setSinglecollegedata(object);
                } else {

                }
            }
            if (count == 8) {

                // System.out.print("Branch name" + line);
                if (!line.contains("NT1") && !line.contains("NT2") && !line.contains("NT3")
                        && !line.contains("OBC")) {
                    if (example.getPrevBranch() != "") {
                        example.getSinglecollegedata().AddObject(example.getCastDataObject());
                        CastDataObject object = new CastDataObject();
                        object.setBranchName(line.substring(11).trim());
                        example.setPrevBranch(line.substring(11).trim());
                        example.setCastDataObject(object);
                    } else {
                        CastDataObject object = new CastDataObject();
                        object.setBranchName(line.substring(11).trim());
                        example.setCastDataObject(object);
                        example.setPrevBranch(line.substring(11).trim());
                    }
                } else {
                    break;
                }
            }
            if (count == 11) {
                // System.out.println("open rank"+line);
                if (isNumeric(line.split(" ")[0])) {
                    if (line.split(" ")[0].trim().equals(null))
                        line = "0";
                    example.getCastDataObject().setOpen(line.split(" ")[0].trim());
                }
            }
            if (count == 14) {
                //   System.out.println("sc rank is"+line);
                if (isNumeric(line)) {
                    if (line.trim().equals(null))
                        line = "0";
                    example.getCastDataObject().setSc(line.trim());
                }

            }
            if (count == 17) {
                // System.out.println("st rank is"+line);
                if (isNumeric(line)) {
                    if (line.trim().equals(null))
                        line = "0";
                    example.getCastDataObject().setSt(line.trim());

                }
            }
            if (count == 20) {
                if (isNumeric(line)) {
                    if (line.trim().equals(null))
                        line = "0";
                    if (prev.equals("GVJO")) {
                        //   System.out.println("VJ rank is"+line);
                        example.getCastDataObject().setVj(line.trim());
                        line = br.readLine();
                        line = br.readLine();
                        line = br.readLine();
                        count += 3;
                        //   System.out.println("NT1 rank is"+line);
                        example.getCastDataObject().setNt1(line.trim());

                    } else {
                        //   System.out.println("NT1 rank is" + line);
                        example.getCastDataObject().setNt1(line.trim());
                        count += 3;
                    }
                }

            }
            //            if (count==23)
            //            {
            //                System.out.println("NT1 rank is"+line);
            //            }
            if (count == 26) {

                if (isNumeric(line)) {
                    //  System.out.println("NT2 rank is"+line);
                    if (line.trim().equals(null))
                        line = "0";
                    example.getCastDataObject().setNt2(line.trim());
                }
            }
            if (count == 29) {

                if (prev.equals("GNT3O")) {
                    //   System.out.println("NT3 rank is" + line);
                    if (isNumeric(line)) {
                        if (line.trim().equals(null))
                            line = "0";
                        example.getCastDataObject().setNt3(line.trim());
                        line = br.readLine();
                        line = br.readLine();
                        line = br.readLine();
                        //    System.out.println("OBC rank is" + line);
                    }
                    if (isNumeric(line.split(" ")[0])) {
                        if (line.split(" ")[0].trim().equals(null))
                            line = "0";
                        example.getCastDataObject().setOBC(line.split(" ")[0].trim());
                    }
                } else {
                    //    System.out.println("OBC rank is" + line);
                    if (isNumeric(line.split(" ")[0])) {
                        if (line.split(" ")[0].trim().equals(null))
                            line = "0";
                        example.getCastDataObject().setOBC(line.split(" ")[0].trim());
                    }
                }

            }
            count++;
            prev = line;
        }
        //                if (count==7)
        //                {
        //                    if (!line.substring(6).equals(example.getPrevCollege()))
        //                    System.out.println("College name is"+line.substring(6));
        //                    example.setPrevCollege(line.substring(6));
        //
        //                }
        //                if (count==8)
        //                {
        //                    if (!line.contains("NT1") && !line.contains("NT2") && !line.contains("NT3") && !line.contains("OBC"))
        //                    System.out.println("Branch name is"+line.substring(12));
        //                }
        //                count++;
        // System.out.println(line);
    }

    //example.getSinglecollegedata().AddObject(example.getCastDataObject());
    example.getSinglecollegedata().AddObject(example.getCastDataObject());
    example.addObject(example.getSinglecollegedata());

    //  System.out.println("Size is" + example.getObject().size());

    //        for (CollegeDataObject collegeDataObject :example.getObject()) {
    //            System.out.println("College Name is " + collegeDataObject.getCollegeName());
    //            for (CastDataObject object : collegeDataObject.getObjects()) {
    //                System.out.println("Branch name is " + object.getBranchName());
    //               // if (isNumeric(object.getOpen()))
    //                System.out.println("Open rank is " + object.getOpen());
    //               // if (isNumeric(object.getSc()))
    //                System.out.println("SC rank is " + object.getSc());
    //              //  if (isNumeric(object.getSt()))
    //                System.out.println("ST rank is " + object.getSt());
    //              //  if (isNumeric(object.getVj()))
    //                System.out.println("VJ rank is " + object.getVj());
    //              //  if (isNumeric(object.getNt1()))
    //                System.out.println("NT1 rank is " + object.getNt1());
    //              //  if (isNumeric(object.getNt2()))
    //                System.out.println("NT2 rank is " + object.getNt2());
    //              //  if (isNumeric(object.getNt3()))
    //                System.out.println("NT3 rank is " + object.getNt3());
    //              //  if (isNumeric(object.getOBC()))
    //                System.out.println("OBC rank is " + object.getOBC());
    //            }
    //        }

    //BufferedReader reader=new Bufferef ll)
    //{
    //   System.out.println(line);
    //}

    MongoClient mongo1 = new MongoClient("localhost");
    MongoDatabase db = mongo1.getDatabase("CollegeFinder");
    MongoCollection<Document> coll = db.getCollection("project");
    for (CollegeDataObject collegeDataObject : example.getObject()) {
        Document college = new Document();
        college.append("college_name", collegeDataObject.getCollegeName());
        college.append("city", collegeDataObject.getCity());
        // System.out.println(collegeDataObject.getCollegeName() + " City "+collegeDataObject.getCity());
        List<Document> branches = new ArrayList<>();
        for (CastDataObject object : collegeDataObject.getObjects()) {
            Document branch = new Document();
            branch.append("branch_name", object.getBranchName());
            branch.append("open", object.getOpen());
            branch.append("sc", object.getSc());
            branch.append("st", object.getSt());
            branch.append("vj", object.getVj());
            branch.append("nt1", object.getNt1());
            branch.append("nt2", object.getNt2());
            branch.append("nt3", object.getNt3());
            branch.append("obc", object.getOBC());
            branches.add(branch);
        }
        college.append("Branch", branches);
        coll.insertOne(college);
    }
    //                m.append("name","sachin");
    //                m.append("year", "first year");
    //                m.append("branch", "seond year");
    //                List <Document> list=new ArrayList<>();
    //                list.add(m);
    //                Document s=new Document();
    //                s.append("name", "Arjun");
    //                s.append("year", "first year");
    //                s.append("branch", "seond year");
    //                list.add(s);
    //                Document parent=new Document();
    //                parent.append("embed", list);
    //                coll.insertOne(parent);

    //              p  
    //                List <Document> list=new ArrayList<>();
    //                for (int i=0;i<10;i++)
    //                {
    //                    list.add(new Document(m));
    //                }
    //                coll.insertMany(list);
    //     coll.insertOne(parent);

    // TODO code application logic here

    //              coll.find();
}

From source file:example.Example.java

public static void main(String[] args) throws IOException {

    String line, prev = "";

    Example example = new Example();
    String[] exclusions = { "100550310 - Food Technology", "100551710 - Oil and Paints Technology",
            "100552410 - Paper and Pulp Technology", "100552710 - Petro Chemical Engineering" };
    //  File file=new File("/home/sachin/Downloads/2014ENGG_Cutoff_CAP1.txt");
    PdfReader reader = new PdfReader("/home/sachin/Downloads/2014ENGG_Cutoff_CAP2.pdf");
    //  System.out.println("This PDF has " + reader.getNumberOfPages() + " pages.");

    for (int i = 1; i < reader.getNumberOfPages(); i += 1) {
        String page = PdfTextExtractor.getTextFromPage(reader, i);
        InputStream is = new ByteArrayInputStream(page.getBytes());
        // read it with BufferedReader
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        int count = 1;
        while ((line = br.readLine()) != null) {
            if (count == 7) {

                //  System.out.println("substring is"+line.substring(6));
                //   System.out.println("Prev college is"+example.getPrevCollege());
                if (!line.substring(6).trim().equals(example.getPrevCollege())) {
                    // System.out.println("College Name" + line);
                    if (example.getPrevCollege() != "") {
                        example.addObject(example.getSinglecollegedata());
                    }//from   ww w  .j  a  va2s .c  o  m
                    example.setPrevCollege(line.substring(6).trim());
                    CollegeDataObject object = new CollegeDataObject();
                    object.setCollegeName(line.substring(6).trim());
                    int temp;
                    if ((temp = line.substring(6).trim().split(",").length) > 1) {
                        object.setCity(line.substring(6).trim().split(",")[temp - 1].trim());
                        object.setCollegeName(line.substring(6).trim()
                                .replace(line.substring(6).trim().split(",")[temp - 1].trim(), "").trim()
                                .replace(",", "").trim());
                    }
                    example.setSinglecollegedata(object);
                } else {

                }
            }
            if (count == 8) {

                // System.out.print("Branch name" + line);
                if (!line.contains("NT1") && !line.contains("NT2") && !line.contains("NT3")
                        && !line.contains("OBC")) {
                    if (example.getPrevBranch() != "") {
                        example.getSinglecollegedata().AddObject(example.getCastDataObject());
                        CastDataObject object = new CastDataObject();
                        object.setBranchName(line.substring(11).trim());
                        example.setPrevBranch(line.substring(11).trim());
                        example.setCastDataObject(object);
                    } else {
                        CastDataObject object = new CastDataObject();
                        object.setBranchName(line.substring(11).trim());
                        example.setCastDataObject(object);
                        example.setPrevBranch(line.substring(11).trim());
                    }
                } else {
                    break;
                }
            }
            if (count == 10) {
                // System.out.println("open rank"+line);

                if (line.contains("GOPENO") || line.contains("GOPENH")) {
                    //                        if (isNumeric(line)) {
                    //                            if (line.trim().equals(null))
                    //                                line = "0";
                    if (line.split(" ").length > 1) {
                        if (isNumeric(line.split(" ")[1].trim()))
                            example.getCastDataObject().setOpen(line.split(" ")[1].trim());
                    } else {
                        line = br.readLine();
                        if (line.split(" ").length > 1) {
                            if (isNumeric(line.split(" ")[1].trim()))
                                example.getCastDataObject().setOpen(line.split(" ")[1].trim());
                        } else {
                            if (isNumeric(line.trim()))
                                example.getCastDataObject().setOpen(line.trim());
                        }
                    }
                    // }
                }

                while ((line = br.readLine()) != null) {
                    if (line.contains("GSCO") || line.contains("GSCH")) {
                        //                            if (isNumeric(line)) {
                        //                                if (line.trim().equals(null))
                        //                                    line = "0";
                        if (line.split(" ").length > 1) {
                            if (isNumeric(line.split(" ")[1].trim()))
                                example.getCastDataObject().setSc(line.split(" ")[1].trim());
                        } else {
                            line = br.readLine();
                            if (line.split(" ").length > 1) {
                                if (isNumeric(line.split(" ")[1].trim()))
                                    example.getCastDataObject().setSc(line.split(" ")[1].trim());
                            } else {
                                if (isNumeric(line.trim()))
                                    example.getCastDataObject().setSc(line.trim());
                            }
                        }

                        //}
                    }
                    if (line.contains("GSTO") || line.contains("GSTH")) {
                        //                            if (isNumeric(line)) {
                        //                                if (line.trim().equals(null))
                        //                                    line = "0";
                        if (line.split(" ").length > 1) {
                            if (isNumeric(line.split(" ")[1].trim()))
                                example.getCastDataObject().setSt(line.split(" ")[1].trim());
                        } else {
                            line = br.readLine();
                            if (line.split(" ").length > 1) {
                                if (isNumeric(line.split(" ")[1].trim()))
                                    example.getCastDataObject().setSt(line.split(" ")[1].trim());
                            } else {
                                if (isNumeric(line.trim()))
                                    example.getCastDataObject().setSt(line.trim());
                            }
                        }
                        //}
                    }
                    if (line.contains("GVJO") || line.contains("GVJH")) {
                        //                            if (isNumeric(line)) {
                        //                                if (line.trim().equals(null))
                        //                                    line = "0";

                        if (line.split(" ").length > 1) {
                            if (isNumeric(line.split(" ")[1].trim()))
                                example.getCastDataObject().setVj(line.split(" ")[1].trim());
                        } else {
                            line = br.readLine();
                            if (line.split(" ").length > 1) {
                                if (isNumeric(line.split(" ")[1].trim()))
                                    example.getCastDataObject().setVj(line.split(" ")[1].trim());
                            } else {
                                if (isNumeric(line.trim()))
                                    example.getCastDataObject().setVj(line.trim());
                            }
                        }
                        //System.out.println("Line is"+line.trim());

                        //  }
                    }
                    if (line.contains("GNT1O") || line.contains("GNT1H")) {
                        //                            if (isNumeric(line)) {
                        //                                if (line.trim().equals(null))
                        //                                    line = "0";
                        if (line.split(" ").length > 1) {
                            if (isNumeric(line.split(" ")[1].trim()))
                                example.getCastDataObject().setNt1(line.split(" ")[1].trim());
                        } else {
                            line = br.readLine();
                            if (line.split(" ").length > 1) {
                                if (isNumeric(line.split(" ")[1].trim()))
                                    example.getCastDataObject().setNt1(line.split(" ")[1].trim());
                            } else {

                                if (isNumeric(line.trim()))
                                    example.getCastDataObject().setNt1(line.trim());
                            }
                        }
                        //                            }

                    }
                    if (line.contains("GNT2O") || line.contains("GNT2H")) {
                        //                            if (isNumeric(line)) {
                        //                                if (line.trim().equals(null))
                        //                                    line = "0";

                        if (line.split(" ").length > 1) {
                            if (isNumeric(line.split(" ")[1].trim()))
                                example.getCastDataObject().setNt2(line.split(" ")[1].trim());
                        } else {
                            line = br.readLine();
                            if (line.split(" ").length > 1) {
                                if (isNumeric(line.split(" ")[1].trim()))
                                    example.getCastDataObject().setNt2(line.split(" ")[1].trim());
                            } else {
                                if (isNumeric(line.trim()))
                                    example.getCastDataObject().setNt2(line.trim());
                            }
                        }
                        //                            }

                    }
                    if (line.contains("GNT3O") || line.contains("GNT3H")) {
                        //                            if (isNumeric(line)) {
                        //                                if (line.trim().equals(null))
                        //                                    line = "0";
                        if (line.split(" ").length > 1) {
                            if (isNumeric(line.split(" ")[1].trim()))
                                example.getCastDataObject().setNt3(line.split(" ")[1].trim());
                        } else {
                            line = br.readLine();
                            if (line.split(" ").length > 1) {
                                if (isNumeric(line.split(" ")[1].trim()))
                                    example.getCastDataObject().setNt3(line.split(" ")[1].trim());
                            } else {
                                if (isNumeric(line.trim()))
                                    example.getCastDataObject().setNt3(line.trim());
                            }
                        }
                        //   }

                    }
                    if (line.contains("GOBCO") || line.contains("GOBCH")) {
                        //                            if (isNumeric(line)) {
                        //                                if (line.trim().equals(null))
                        //                                    line = "0";
                        if (line.split(" ").length > 1) {
                            if (isNumeric(line.split(" ")[1].trim()))
                                example.getCastDataObject().setOBC(line.split(" ")[1].trim());
                        } else {
                            line = br.readLine();
                            if (line.split(" ").length > 1) {
                                if (isNumeric(line.split(" ")[1].trim()))
                                    example.getCastDataObject().setOBC(line.split(" ")[1].trim());
                            } else {
                                if (isNumeric(line.trim()))
                                    example.getCastDataObject().setOBC(line.trim());
                            }
                        }
                        //    }
                    }

                }

            }
            //                if (count == 14) {
            //                    //   System.out.println("sc rank is"+line);
            //                    if (isNumeric(line)) {
            //                        if (line.trim().equals(null))
            //                            line = "0";
            //                        example.getCastDataObject().setSc(line.trim());
            //                    }
            //
            //
            //                }
            //                if (count == 17) {
            //                    // System.out.println("st rank is"+line);
            //                    if (isNumeric(line)) {
            //                        if (line.trim().equals(null))
            //                            line = "0";
            //                        example.getCastDataObject().setSt(line.trim());
            //
            //                    }
            //                }
            //                if (count == 20) {
            //                    if (isNumeric(line)) {
            //                        if (line.trim().equals(null))
            //                            line = "0";
            //                        if (prev.equals("GVJO")) {
            //                            //   System.out.println("VJ rank is"+line);
            //                            example.getCastDataObject().setVj(line.trim());
            //                            line = br.readLine();
            //                            line = br.readLine();
            //                            line = br.readLine();
            //                            count += 3;
            //                            //   System.out.println("NT1 rank is"+line);
            //                            example.getCastDataObject().setNt1(line.trim());
            //
            //                        } else {
            //                            //   System.out.println("NT1 rank is" + line);
            //                            example.getCastDataObject().setNt1(line.trim());
            //                            count += 3;
            //                        }
            //                    }
            //
            //                }
            //            if (count==23)
            //            {
            //                System.out.println("NT1 rank is"+line);
            //            }
            //                if (count == 26) {
            //
            //                    if (isNumeric(line)) {
            //                        //  System.out.println("NT2 rank is"+line);
            //                        if (line.trim().equals(null))
            //                            line="0";
            //                        example.getCastDataObject().setNt2(line.trim());
            //                    }
            //                }
            //                if (count == 29) {
            //
            //                        if (prev.equals("GNT3O")) {
            //                            //   System.out.println("NT3 rank is" + line);
            //                            if (isNumeric(line)) {
            //                                if (line.trim().equals(null))
            //                                    line="0";
            //                                example.getCastDataObject().setNt3(line.trim());
            //                                line = br.readLine();
            //                                line = br.readLine();
            //                                line = br.readLine();
            //                                //    System.out.println("OBC rank is" + line);
            //                            }
            //                            if (isNumeric(line.split(" ")[0])) {
            //                                if (line.split(" ")[0].trim().equals(null))
            //                                    line="0";
            //                                example.getCastDataObject().setOBC(line.split(" ")[0].trim());
            //                            }
            //                        } else {
            //                            //    System.out.println("OBC rank is" + line);
            //                            if (isNumeric(line.split(" ")[0])) {
            //                                if (line.split(" ")[0].trim().equals(null))
            //                                    line = "0";
            //                                example.getCastDataObject().setOBC(line.split(" ")[0].trim());
            //                            }
            //                        }

            //               }
            count++;
            prev = line;
        }
    }
    //                if (count==7)
    //                {
    //                    if (!line.substring(6).equals(example.getPrevCollege()))
    //                    System.out.println("College name is"+line.substring(6));
    //                    example.setPrevCollege(line.substring(6));
    //
    //                }
    //                if (count==8)
    //                {
    //                    if (!line.contains("NT1") && !line.contains("NT2") && !line.contains("NT3") && !line.contains("OBC"))
    //                    System.out.println("Branch name is"+line.substring(12));
    //                }
    //                count++;
    //                System.out.println(line);
    //            }
    //        }

    //example.getSinglecollegedata().AddObject(example.getCastDataObject());
    example.getSinglecollegedata().AddObject(example.getCastDataObject());
    example.addObject(example.getSinglecollegedata());

    //  System.out.println("Size is" + example.getObject().size());

    //        for (CollegeDataObject collegeDataObject :example.getObject()) {
    //            System.out.println("College Name is " + collegeDataObject.getCollegeName());
    //            for (CastDataObject object : collegeDataObject.getObjects()) {
    //                System.out.println("Branch name is " + object.getBranchName());
    //               // if (isNumeric(object.getOpen()))
    //                System.out.println("Open rank is " + object.getOpen());
    //               // if (isNumeric(object.getSc()))
    //                System.out.println("SC rank is " + object.getSc());
    //              //  if (isNumeric(object.getSt()))
    //                System.out.println("ST rank is " + object.getSt());
    //              //  if (isNumeric(object.getVj()))
    //                System.out.println("VJ rank is " + object.getVj());
    //              //  if (isNumeric(object.getNt1()))
    //                System.out.println("NT1 rank is " + object.getNt1());
    //              //  if (isNumeric(object.getNt2()))
    //                System.out.println("NT2 rank is " + object.getNt2());
    //              //  if (isNumeric(object.getNt3()))
    //                System.out.println("NT3 rank is " + object.getNt3());
    //              //  if (isNumeric(object.getOBC()))
    //                System.out.println("OBC rank is " + object.getOBC());
    //            }
    //        }

    //BufferedReader reader=new Bufferef ll)
    //{
    //   System.out.println(line);
    //}

    MongoClient mongo1 = new MongoClient("localhost");
    MongoDatabase db = mongo1.getDatabase("CollegeFinder");
    MongoCollection<Document> coll = db.getCollection("cap_round2");
    for (CollegeDataObject collegeDataObject : example.getObject()) {
        Document college = new Document();
        college.append("college_name", collegeDataObject.getCollegeName());
        college.append("city", collegeDataObject.getCity());
        System.out.println(collegeDataObject.getCollegeName() + " City " + collegeDataObject.getCity());
        List<Document> branches = new ArrayList<>();
        for (CastDataObject object : collegeDataObject.getObjects()) {
            Document branch = new Document();
            branch.append("branch_name", object.getBranchName());
            branch.append("open", Integer.valueOf(object.getOpen()));
            branch.append("sc", Integer.valueOf(object.getSc()));
            branch.append("st", Integer.valueOf(object.getSt()));
            branch.append("vj", Integer.valueOf(object.getVj()));
            branch.append("nt1", Integer.valueOf(object.getNt1()));
            branch.append("nt2", Integer.valueOf(object.getNt2()));
            branch.append("nt3", Integer.valueOf(object.getNt3()));
            branch.append("obc", Integer.valueOf(object.getOBC()));
            branches.add(branch);
        }
        college.append("Branch", branches);
        coll.insertOne(college);
    }
    //                m.append("name","sachin");
    //                m.append("year", "first year");
    //                m.append("branch", "seond year");
    //                List <Document> list=new ArrayList<>();
    //                list.add(m);
    //                Document s=new Document();
    //                s.append("name", "Arjun");
    //                s.append("year", "first year");
    //                s.append("branch", "seond year");
    //                list.add(s);
    //                Document parent=new Document();
    //                parent.append("embed", list);
    //                coll.insertOne(parent);

    //              p
    //                List <Document> list=new ArrayList<>();
    //                for (int i=0;i<10;i++)
    //                {
    //                    list.add(new Document(m));
    //                }
    //                coll.insertMany(list);
    //     coll.insertOne(parent);

    // TODO code application logic here

    //              coll.find();

}

From source file:examples.tour.QuickTour.java

License:Apache License

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes an optional single argument for the connection string
 *//*  ww  w  .j  a  va2  s  . c o m*/
public static void main(final String[] args) {
    MongoClient mongoClient;

    if (args.length == 0) {
        // connect to the local database server
        mongoClient = new MongoClient();
    } else {
        mongoClient = new MongoClient(new MongoClientURI(args[0]));
    }

    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb");

    // get a handle to the "test" collection
    MongoCollection<Document> collection = database.getCollection("test");

    // drop all the data in it
    collection.drop();

    // make a document and insert it
    Document doc = new Document("name", "MongoDB").append("type", "database").append("count", 1).append("info",
            new Document("x", 203).append("y", 102));

    collection.insertOne(doc);

    // get it (since it's the only one in there since we dropped the rest earlier on)
    Document myDoc = collection.find().first();
    System.out.println(myDoc.toJson());

    // now, lets add lots of little documents to the collection so we can explore queries and cursors
    List<Document> documents = new ArrayList<Document>();
    for (int i = 0; i < 100; i++) {
        documents.add(new Document("i", i));
    }
    collection.insertMany(documents);
    System.out.println(
            "total # of documents after inserting 100 small ones (should be 101) " + collection.count());

    // find first
    myDoc = collection.find().first();
    System.out.println(myDoc.toJson());

    // lets get all the documents in the collection and print them out
    MongoCursor<Document> cursor = collection.find().iterator();
    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next().toJson());
        }
    } finally {
        cursor.close();
    }

    for (Document cur : collection.find()) {
        System.out.println(cur.toJson());
    }

    // now use a query to get 1 document out
    myDoc = collection.find(eq("i", 71)).first();
    System.out.println(myDoc.toJson());

    // now use a range query to get a larger subset
    cursor = collection.find(gt("i", 50)).iterator();

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next().toJson());
        }
    } finally {
        cursor.close();
    }

    // range query with multiple constraints
    cursor = collection.find(and(gt("i", 50), lte("i", 100))).iterator();

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next().toJson());
        }
    } finally {
        cursor.close();
    }

    // Query Filters
    myDoc = collection.find(eq("i", 71)).first();
    System.out.println(myDoc.toJson());

    // now use a range query to get a larger subset
    Block<Document> printBlock = new Block<Document>() {

        public void apply(final Document document) {
            System.out.println(document.toJson());
        }
    };
    collection.find(gt("i", 50)).forEach(printBlock);

    // filter where; 50 < i <= 100
    collection.find(and(gt("i", 50), lte("i", 100))).forEach(printBlock);

    // Sorting
    myDoc = collection.find(exists("i")).sort(descending("i")).first();
    System.out.println(myDoc.toJson());

    // Projection
    myDoc = collection.find().projection(excludeId()).first();
    System.out.println(myDoc.toJson());

    // Aggregation
    collection
            .aggregate(
                    asList(match(gt("i", 0)), project(Document.parse("{ITimes10: {$multiply: ['$i', 10]}}"))))
            .forEach(printBlock);

    myDoc = collection.aggregate(singletonList(group(null, sum("total", "$i")))).first();
    System.out.println(myDoc.toJson());

    // Update One
    collection.updateOne(eq("i", 10), set("i", 110));

    // Update Many
    UpdateResult updateResult = collection.updateMany(lt("i", 100), inc("i", 100));
    System.out.println(updateResult.getModifiedCount());

    // Delete One
    collection.deleteOne(eq("i", 110));

    // Delete Many
    DeleteResult deleteResult = collection.deleteMany(gte("i", 100));
    System.out.println(deleteResult.getDeletedCount());

    collection.drop();

    // ordered bulk writes
    List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
    writes.add(new InsertOneModel<Document>(new Document("_id", 4)));
    writes.add(new InsertOneModel<Document>(new Document("_id", 5)));
    writes.add(new InsertOneModel<Document>(new Document("_id", 6)));
    writes.add(
            new UpdateOneModel<Document>(new Document("_id", 1), new Document("$set", new Document("x", 2))));
    writes.add(new DeleteOneModel<Document>(new Document("_id", 2)));
    writes.add(new ReplaceOneModel<Document>(new Document("_id", 3), new Document("_id", 3).append("x", 4)));

    collection.bulkWrite(writes);

    collection.drop();

    collection.bulkWrite(writes, new BulkWriteOptions().ordered(false));
    //collection.find().forEach(printBlock);

    // Clean up
    database.drop();

    // release resources
    mongoClient.close();
}

From source file:flipkart.mongo.replicator.node.ReplicationTask.java

License:Apache License

@Override
public void run() {
    String shardId = rsConfig.shardName;
    Node masterNode = rsConfig.getMasterNode().get();
    MongoClient client = MongoConnector.getMongoClient(Lists.newArrayList(masterNode));

    MongoDatabase database = client.getDatabase("local");
    lastCp = taskContext.checkPointHandler.getCheckPoint(shardId);

    logger.info(String.format("######## START REPLICATOR FOR MongoURI: %s. LastCheckpoint: %s #######",
            client.getAddress(), lastCp));
    MongoCollection<Document> collection = database.getCollection("oplog.rs");
    FindIterable<Document> iterable;
    MongoCursor<Document> cursor;
    do {//from   w ww. ja va  2 s.c o m
        if (lastCp == null) {
            iterable = collection.find();
        } else {
            iterable = collection.find(new Document("ts", new Document("$gt", lastCp)));
        }
        cursor = iterable.sort(new Document("$natural", 1)).noCursorTimeout(true)
                .cursorType(CursorType.TailableAwait).batchSize(3000).iterator();
        try {
            executeCursor(cursor);
            Thread.sleep(WAIT_FOR_NEXT_ITERATION);
        } catch (MongoCursorNotFoundException e) {
            logger.info("Cursor has been closed. About to open a new cursor. ID: "
                    + cursor.getServerCursor().getId());
        } catch (Exception e) {
            logger.error("Exception while replicating", e);
            throw new RuntimeException(e);
        } finally {
            cursor.close();
        }
    } while (true);
}

From source file:fr.utbm.repository.MongoDBDao.java

public void subscribeClientToCourseSession(Client cl) {

    MongoClient mongoClient = null;/*from  w w w .ja v a2s.com*/

    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.utils.MongoDBUtil.java

License:Apache License

public <T> T doJob(MongoDBCallback<T> callback) {
    MongoClient client = null;//from  ww  w . j  a va2  s .co  m
    T t = null;
    try {
        client = new MongoClient(host, port);
        MongoDatabase database = client.getDatabase(databaseName);
        MongoCollection<Document> collection = database.getCollection(collectionName);
        t = callback.process(collection);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return t;
}

From source file:geriapp.controller.BootstrapController.java

public static String importJSONFileToDB(String pathToFile, MongoDatabase db, String collectionName) {
    // open file/*from  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;/*from   w  w  w. j  a  v  a 2  s. c  om*/

    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);
    }

    mongo.close();
}

From source file:geriapp.dao.ReadingDAO.java

public static Reading getLatestReading(String type) {//Change later to ID instead of type
    MongoClient mongo = new MongoClient("54.254.204.169", 27017);

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

    MongoCollection<Document> newColl;

    Gson gson = new Gson();

    if (type.equals("medbox")) {
        newColl = db.getCollection("Medbox");
        Document latestEntry = newColl.find().iterator().next();
        String json = latestEntry.toJson();
        Reading reading = gson.fromJson(json, MedboxReading.class);
        mongo.close();/*from   w  w w . ja v a 2s  .co m*/
        return reading;
    } else if (type.equals("door")) {

    }
    return null; //throw Exception??
}