List of usage examples for com.mongodb.client MongoCollection insertOne
void insertOne(TDocument document);
From source file:entities.fertilizacion.SubanalisisFertilizacion.java
public ObjectId save() { MongoManager mongo = MongoManager.getInstance(); MongoCollection table = mongo.db.getCollection("subanalisis_fertilizacion"); Document obj = new Document("simbolo", this.simbolo).append("nombre", this.nombre); table.insertOne(obj); return (ObjectId) obj.get("_id"); }
From source file:entities.fertilizacion.TipoAplicacionFuente.java
public ObjectId save() { MongoManager mongo = MongoManager.getInstance(); MongoCollection table = mongo.db.getCollection("tipoAplicacionFuente"); Document obj = new Document("nombre", this.nombre.toUpperCase()); table.insertOne(obj); return (ObjectId) obj.get("_id"); }
From source file:entities.fertilizacion.UnidadMedida.java
public ObjectId save() { MongoManager mongo = MongoManager.getInstance(); MongoCollection table = mongo.db.getCollection("unidadMedida"); Document obj = new Document("nombre", this.nombre); table.insertOne(obj); return (ObjectId) obj.get("_id"); }
From source file:entities.fertilizacion.Variedad.java
public ObjectId save() { MongoManager mongo = MongoManager.getInstance(); MongoCollection table = mongo.db.getCollection("variedad"); Document obj = new Document("nombre", this.nombre.toUpperCase()).append("cultivo", this.cultivo); table.insertOne(obj); return (ObjectId) obj.get("_id"); }
From source file:entities.reportes.TextoReporteResultadoLaboratorio.java
public ObjectId save() { MongoManager mongo = MongoManager.getInstance(); MongoCollection table = mongo.db.getCollection("textoReporteResultadoLaboratorio"); Document obj = new Document("referencia", this.referencia.toUpperCase()).append("descripcion", this.descripcion); table.insertOne(obj); return (ObjectId) obj.get("_id"); }
From source file:entities.Usuario.java
public void save() { MongoManager mongo = MongoManager.getInstance(); MongoCollection table = mongo.db.getCollection("usuario"); Document obj = new Document("nombre", this.nombre).append("password", this.password) .append("tipo", this.tipo).append("email", this.email); table.insertOne(obj); }
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 . ja va 2 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()); }// www.jav a2 s . c om 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 *//*from w w w . j a va 2 s .c om*/ 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:fr.lirmm.graphik.graal.keyval.KeyValueStoreMongoDB.java
License:Open Source License
public void importJsonIntoCollection(String collname, String jsonFile) throws IOException { MongoCollection<Document> collection = this.db.getCollection(collname); BufferedReader reader = new BufferedReader(new FileReader(jsonFile)); try {// w ww .j a va2 s. co m String json; while ((json = reader.readLine()) != null) { collection.insertOne(Document.parse(json)); } } finally { reader.close(); } }