List of usage examples for com.mongodb.client MongoCollection insertOne
void insertOne(TDocument document);
From source file:StartScreen.java
private void insertMongo() throws Exception { // *****This is to connect to database**// MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("database"); MongoCollection<Document> elexirCollection = db.getCollection("test"); // *********This is to connect to the database***********// //To clear out existing files from mongo db.getCollection("test").deleteMany(new Document()); // *******This is to read the file into program*********// //String fileDirectory = chooser.getCurrentDirectory() + ""; FileReader file = new FileReader(pathField.getText()); BufferedReader reader = new BufferedReader(file); String line = reader.readLine(); // **********This is to read the text file into program***************// // Creating the Array List to store types of variables List<String> Types = new ArrayList<String>(); List<String> Objects = new ArrayList<String>(); List<String> Predicates = new ArrayList<String>(); List<String> Cats = new ArrayList<String>(); List<String> Category = new ArrayList<String>(); List<String> Action = new ArrayList<String>(); List<Double> DerivProb = new ArrayList<Double>(); List<Double> RootProb = new ArrayList<Double>(); List<String> InitialState = new ArrayList<String>(); List<String> FinalState = new ArrayList<String>(); List<String> Roots = new ArrayList<String>(); List<String> Probability = new ArrayList<String>(); ArrayList<Document> Doc = new ArrayList<Document>(); // initialize all types of definitions String type = null;/*from w ww . ja va2s . c om*/ String object = null; String predicate = null; String cat = null; String category = null; String action = null; String derivProb = null; String rootProb = null; String initialState = null; String finalState = null; String roots = null; String probability = null; Document Exp = new Document(); Document definitions = new Document(); Document stats = new Document(); // Read type Definitions while (line != null) { if (line.contains("Defined Type: ")) { int startingIndexOfType; String types = "Defined Type: "; startingIndexOfType = line.indexOf("Defined Type: "); int endingIndexOfType = line.indexOf("."); type = line.substring(startingIndexOfType + types.length(), endingIndexOfType); // putting the piece of string into the new string Types.add(type); } // Read object definitions. else if (line.contains("Defined Object: ")) { int startingIndexOfObj; // this is to split each word from its // spaces and print word by word String objects = "Defined Object: "; startingIndexOfObj = line.indexOf("Defined Object: "); int endingIndexOfObj = line.indexOf("."); object = line.substring(startingIndexOfObj + objects.length(), endingIndexOfObj); // putting the piece of string into the new string Objects.add(object); } // Read predicate definitions. else if (line.contains("Defined predicate:")) { String predicates = "Defined predicate:"; int startingIndexOfPred; // this is to split each word from its // spaces and print word by word startingIndexOfPred = line.indexOf("Defined predicate:"); int endingIndexOfPred = line.indexOf("."); predicate = line.substring(startingIndexOfPred + predicates.length(), endingIndexOfPred); // putting the piece of string into the new string Predicates.add(predicate); } // Defined Cat-Definition else if (line.contains("Defined: Cat-Definition: ")) { int startingIndexOfCat; // this is to split each word from its // spaces and print word by word String catDef = "Defined: Cat-Definition: "; startingIndexOfCat = line.indexOf("Defined: Cat-Definition: "); int endingIndexOfCat = line.indexOf(".", startingIndexOfCat); cat = line.substring(startingIndexOfCat + catDef.length(), endingIndexOfCat); // putting the piece of string into the new string Cats.add(cat); } // Defined Action Definitions else if (line.contains("Defined: category: ")) { String categ = "Defined: category: "; int startingIndexOfCategory = line.indexOf("Defined: category: "); int endingIndexOfCategory = line.indexOf(";", startingIndexOfCategory); category = line.substring(startingIndexOfCategory + categ.length(), endingIndexOfCategory); Category.add(category); } else if (line.contains("Defined: ")) { // this is to split each word from its // spaces and print word by word String defined = "Defined: "; int startingIndexOfAct = line.indexOf("Defined: "); int endingIndexOfAct = line.indexOf(".", startingIndexOfAct); action = line.substring(startingIndexOfAct + defined.length(), endingIndexOfAct); // putting the piece of string into the new string Action.add(action); } line = reader.readLine(); definitions = new Document(); if (line.startsWith("Read goals for query.")) break; } definitions.append("Types", Types).append("Objects", Objects).append("Predicates", Predicates) .append("Cats", Cats).append("Category", Category).append("Actions", Action); elexirCollection.insertOne(definitions); while (line != null) { DerivProb = new ArrayList<Double>(); RootProb = new ArrayList<Double>(); // To clear out the array so that InitialState = new ArrayList<String>(); // the document does not repeat with FinalState = new ArrayList<String>(); // roots from previous explanations Roots = new ArrayList<String>(); if (line.startsWith("[Exp:")) { String dp = "derivProb :"; int startingIndexOfDP; startingIndexOfDP = line.indexOf("derivProb: "); int endingIndexOfDP = line.indexOf(" root", startingIndexOfDP); derivProb = line.substring(startingIndexOfDP + dp.length(), endingIndexOfDP); DerivProb.add(Double.parseDouble(derivProb)); String rp = "rootProb :"; int startingIndexOfRP; startingIndexOfRP = line.indexOf("rootProb: "); int endingIndexOfRP = line.indexOf(" Initial", startingIndexOfRP); rootProb = line.substring(startingIndexOfRP + rp.length(), endingIndexOfRP); RootProb.add(Double.parseDouble(rootProb)); String is = "Initial State:[ "; int startingIndexOfIS; startingIndexOfIS = line.indexOf("Initial State:[ "); int endingIndexOfIS = line.indexOf(" ]", startingIndexOfIS); initialState = line.substring(startingIndexOfIS + is.length(), endingIndexOfIS); InitialState.add(initialState); String fs = "Final State:[ "; int startingIndexOfFS; startingIndexOfFS = line.indexOf("Final State:[ "); int endingIndexOfFS = line.indexOf(" ]", startingIndexOfFS); finalState = line.substring(startingIndexOfFS + fs.length(), endingIndexOfFS); // capture last FinalState.add(finalState); String rootStr = "root:["; for (int x = line.indexOf(rootStr); x > -1; x = line.indexOf(rootStr, ++x)) { int endingIndexOfRoot = line.indexOf("]", x); roots = line.substring(x + rootStr.length(), endingIndexOfRoot); Roots.add(roots); } Exp = new Document(); Exp.append("derivProb", Arrays.asList(derivProb)).append("rootProb", Arrays.asList(rootProb)) .append("initialState", Arrays.asList(initialState)) .append("finalState", Arrays.asList(finalState)).append("Roots", Roots); Doc.add(Exp); elexirCollection.insertOne(Exp); } else if (!line.startsWith("[Exp:")) { //System.out.println("At first if else"); // System.out.println(line); if (line.contains("Probabilites:")) { //System.out.println("At line equals prob"); while (!line.contains("*** Done with problem. ***")) { //System.out.println("Reached while loop"); //read each line and save here probability += line + "\n"; //System.out.println(probability); line = reader.readLine(); } Probability.add(probability); stats.append("Probability", Probability); elexirCollection.insertOne(stats); } } line = reader.readLine(); } // end while FindIterable<Document> iter = elexirCollection.find(); System.out.println("Your Documents have been stored into mongoDB "); }
From source file:WebCategorizer.java
public void getWebCategories(HashSet<String> urlSet, HashMap<String, String> websiteCategoryMap, MongoCollection<org.bson.Document> websiteCategoryCollection) { try {/* ww w. jav a2 s .c o m*/ File file = new File(categoriesFile); JSONObject webCatList = new JSONObject(); if (file.exists() && file.length() != 0) { webCatList = loadCategories(); } else { webCatList = fetchCategories(); } for (String url : urlSet) { if (websiteCategoryMap.containsKey(url)) continue; org.bson.Document urlDoc = websiteCategoryCollection.find(eq("URLDomain", url)).first(); if (urlDoc == null) { String host = ""; String port = ""; if (url.indexOf(':') > -1) { String[] urlAddr = url.split(":"); host = urlAddr[0]; port = urlAddr[1]; } else { host = url; port = "80"; } StringBuilder remoteUrl = new StringBuilder("http://sp.cwfservice.net/1/R/"); remoteUrl.append(k9License); remoteUrl.append("/K9-00006/0/GET/HTTP/"); remoteUrl.append(host); remoteUrl.append("/"); remoteUrl.append(port); remoteUrl.append("///"); //Fetch Key for URL URL obj = new URL(remoteUrl.toString()); URLConnection connection = obj.openConnection(); Document doc = parseXML(connection.getInputStream()); NodeList descNodes = doc.getElementsByTagName("DomC"); if (descNodes.getLength() != 0) { String cat = (String) webCatList .get(breakString(descNodes.item(0).getTextContent().toLowerCase())); websiteCategoryMap.put(url, cat); websiteCategoryCollection.insertOne(new org.bson.Document("URLId", url.hashCode()) .append("URLDomain", url.toString()).append("URLCategory", cat)); } else { descNodes = doc.getElementsByTagName("DirC"); String cat = (String) webCatList .get(breakString(descNodes.item(0).getTextContent().toLowerCase())); websiteCategoryMap.put(url, cat); websiteCategoryCollection.insertOne(new org.bson.Document("URLId", url.hashCode()) .append("URLDomain", url.toString()).append("URLCategory", cat)); } } else { websiteCategoryMap.put(url, new JSONObject(urlDoc.toJson()).get("URLCategory").toString()); } } } catch (Exception ex) { System.out.println(ex.getClass().getName()); System.out.println("Not working"); } }
From source file:Register.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: MongoClient client = new MongoClient("localhost", 27017); MongoDatabase db = client.getDatabase("ExpenseManager"); MongoCollection<Document> reg = db.getCollection("Registration"); String unm = jTextField2.getText(); BasicDBObject query = new BasicDBObject("unm", unm); String pass = String.valueOf(jPasswordField1.getPassword()); String c_pass = String.valueOf(jPasswordField2.getPassword()); if (unm.length() == 0) { JOptionPane.showMessageDialog(null, "User Name is empty"); this.setVisible(true); }//from w ww . j a v a 2 s .co m else if (reg.find(query).iterator().hasNext()) { JOptionPane.showMessageDialog(null, "User Name already exist"); this.setVisible(true); } else if (jPasswordField1.getPassword().length == 0 || jPasswordField2.getPassword().length == 0) { JOptionPane.showMessageDialog(null, "Password Field Can not be empty"); this.setVisible(true); } else if (!pass.equals(c_pass)) { JOptionPane.showMessageDialog(null, "Passwords do not match. Please Re-enter the password"); this.setVisible(true); } else { String name = jTextField1.getText(); Document ins = new Document("name", name).append("unm", unm).append("pass", pass); reg.insertOne(ins); System.out.println("Document is inserted into MongoDb"); System.out.println(pass); db.createCollection(unm); this.dispose(); login lgn = new login(); lgn.setVisible(true); } }
From source file:DataBaseInteraction.java
public static void insertDocument(Document document, MongoCollection collection) { collection.insertOne(document); }
From source file:anuncius.singleton.AnunciusDAO.java
public boolean save(AbstractRequest request) { if (request != null && request.hasValidData()) { MongoCollection<Document> authCollection = mongo.getCollection(request.getCollectionName()); Document dbObject = request.convertToMongoObject(); authCollection.insertOne(dbObject); return true; }/* ww w . ja v a 2 s . c o m*/ return false; }
From source file:anuncius.singleton.MongoHandler.java
public Document save(Document d, String collectionName) { MongoCollection<Document> collection = mainDatabase.getCollection(collectionName); d.put("deleted", false); collection.insertOne(d); return d;/*from w ww .ja v a 2 s.com*/ }
From source file:ARS.DBManager.java
public void insertUser(User uObj) { try {/*from w w w. java2s . co m*/ MongoClient mongoClient = new MongoClient("localhost", 27017); //Connecting MongoDatabase db = mongoClient.getDatabase("ars"); System.out.println("Connecting to the db..."); MongoCollection collection = db.getCollection("users"); //Choosing the collection to insert System.out.println(collection); Document inputObj = new Document(); // inputObj.put("_id", 1123); inputObj.put("first-name", uObj.getFirstName()); inputObj.put("last-name", uObj.getLastName()); inputObj.put("gender", uObj.getGender()); inputObj.put("nationality", uObj.getNation()); inputObj.put("phone-number", uObj.getPhoneNumber()); inputObj.put("email", uObj.geteMail()); inputObj.put("password", uObj.getPassword()); inputObj.put("user-type", "customer"); collection.insertOne(inputObj); } catch (Exception e) { e.printStackTrace(); } }
From source file:ARS.DBManager.java
public void insertFlight(Flight fObj) { try {/*w w w . j ava 2 s . c o m*/ MongoClient mongoClient = new MongoClient("localhost", 27017); //Connecting MongoDatabase db = mongoClient.getDatabase("ars"); System.out.println("Connecting to the db..."); MongoCollection collection = db.getCollection("flight"); //Choosing the collection to insert System.out.println(collection); Document flightObj = new Document(); flightObj.put("fNO", 1001); flightObj.put("departure", "Izmir"); flightObj.put("arrival", "Istanbul"); flightObj.put("date", "2017-05-08 12:00:00"); //TODO: Change this dummy date. collection.insertOne(flightObj); } catch (Exception e) { e.printStackTrace(); } }
From source file:ARS.DBManager.java
public void insertPlane(Plane pObj) { try {/*from w w w.j a v a2s . com*/ MongoClient mongoClient = new MongoClient("localhost", 27017); //Connecting MongoDatabase db = mongoClient.getDatabase("ars"); System.out.println("Connecting to the db..."); MongoCollection collection = db.getCollection("plane"); //Choosing the collection to insert System.out.println(collection); List<Object> seatDBList = new BasicDBList(); Document planeObj = new Document(); planeObj.put("pNo", 2); planeObj.put("flightNo", 2); planeObj.put("Type", "Airbus A380"); for (Seat seatList : pObj.getSeatVector()) //TODO: Change it { DBObject seatDBObject = new BasicDBObject(); seatDBObject.put("sNo", seatList.getSeatNumber()); seatDBObject.put("isEmpty", seatList.getIsEmpty()); seatDBObject.put("isEconomy", seatList.getIsEconomy()); seatDBList.add(seatDBObject); } planeObj.put("Seats", seatDBList); collection.insertOne(planeObj); } catch (Exception e) { e.printStackTrace(); } }
From source file:bariopendatalab.db.DBAccess.java
public void insert(String type, String description, String address, String note, Document loc) { Document doc = new Document("type", type); if (description != null) { doc.append("description", description); }//from w w w . j a va 2s . c o m if (address != null) { doc.append("address", address); } if (note != null) { doc.append("note", note); } if (loc != null) { loc.append("loc", loc); } MongoDatabase database = client.getDatabase(DBNAME); MongoCollection<Document> collection = database.getCollection(COLLNAME); collection.insertOne(doc); }