List of usage examples for com.mongodb DBCollection find
public DBCursor find(final DBObject query)
From source file:DataAccess.DAO.LoginDAO.java
public String validate(String username, String password) { String returnText = LOGIN_FAILURE; Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("Restaurant"); try {/* w w w . java 2 s . c o m*/ //boolean auth = db.authenticate(username, password.toCharArray()); MongoCredential credential2 = MongoCredential.createCredential(username, "Restaurant", password.toCharArray()); MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential2)); DB db2 = mongoClient.getDB("Restaurant"); System.out.println("Connect to database successfully"); DBCollection coll = db2.getCollection("Users"); System.out.println("Collection users selected successfully"); BasicDBObject document2 = new BasicDBObject(); document2.put("UserName", username); document2.put("Password", password); //coll.insert(document2); DBCursor cur2 = coll.find(document2); System.out.println(cur2.toString()); while (cur2.hasNext()) { System.out.println(cur2.next()); } System.out.println("Login is successful!"); if (credential2 != null) { DBCollection table = db.getCollection("Users"); BasicDBObject document = new BasicDBObject(); document.put("UserName", username); table.insert(document); DBCursor cur = table.find(document); while (cur.hasNext()) { System.out.println(cur.next()); } HttpSession session = SessionBean.getSession(); session.setAttribute("UserName", user); System.out.println("Login is successful!"); returnText = LOGIN_SUCCESS; return "admins"; } else { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Incorrect Username and Passowrd", "Please enter correct username and Password")); System.out.println("Login is failed!"); return "login"; } //System.out.println("Done"); //return returnText; } catch (MongoException e) { e.printStackTrace(); } finally { mongo.close(); } return returnText; }
From source file:datapreparation.MongoStatistics.java
public void usersToUrls() { // To directly connect to a single MongoDB server (note that this will not auto-discover the primary even MongoClient mongoClient;/*from ww w . ja va2s .c o m*/ try { mongoClient = new MongoClient("localhost"); //use database DB db = mongoClient.getDB("users"); //get collection DBCollection coll = db.getCollection("urls"); //iterate with a cursor BasicDBObject query = new BasicDBObject("source", new BasicDBObject("$exists", true)); DBCursor cursor = coll.find(query); BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter("Users_To_Urls.txt")); writer.write("url,user,time\n"); while (cursor.hasNext()) { BasicDBObject tweet = (BasicDBObject) cursor.next(); String time = tweet.get("created_at").toString(); String user = tweet.get("from_user").toString(); String urls[] = tweet.get("source").toString().replaceAll(""", "").split(";"); for (String url : urls) { if (url.matches("http.*")) { //The user posted one link, write it in the file! writer.write(url + "," + user + "," + time + "\n"); } } } } finally { cursor.close(); mongoClient.close(); writer.close(); } } catch (IOException ex) { System.out.println("Something's Wrong! " + ex); } }
From source file:de.otto.mongodb.profiler.op.OpProfileDataFetcher.java
License:Apache License
private DBCursor getCursor() { synchronized (cursorMutex) { // Close stale cursor if (cursor != null && cursor.getCursorId() == 0L) { cursor.close();//from www . ja v a2 s .com cursor = null; } // Create new cursor if (cursor == null && db.collectionExists(COLLECTION)) { if (lastTs == null) { lastTs = DateTime.now(DateTimeZone.UTC); } final DBCollection collection = db.getCollection(COLLECTION); final DBObject query = QueryBuilder.start() .and(QueryBuilder.start("ns").notEquals(collection.getFullName()).get(), QueryBuilder.start("ts").greaterThan(lastTs.toDate()).get()) .get(); final DBObject sortBy = new BasicDBObject("$natural", 1); final DBCursor cursor = collection.find(query).sort(sortBy).batchSize(100) .addOption(Bytes.QUERYOPTION_TAILABLE).addOption(Bytes.QUERYOPTION_AWAITDATA); this.cursor = cursor; } } return cursor; }