Example usage for com.mongodb DBCollection find

List of usage examples for com.mongodb DBCollection find

Introduction

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

Prototype

public DBCursor find() 

Source Link

Document

Select all documents in collection and get a cursor to the selected documents.

Usage

From source file:com.smartlearner.email.analytics.dbmanage.java

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

    Mongo mongoClient = new Mongo("localhost", 27017);

    DB db = mongoClient.getDB("header");
    System.out.println("Conn succesful");

    DBCollection coll3 = db.getCollection("head_coll");
    System.out.println("Collection created successfully ...");

    /* BasicDBObject document = new BasicDBObject();
    document.put("subject", "sub");/*from   w  ww  .j a  va 2 s  .co m*/
    document.put("from","frm");
    document.put("date","dt");
    coll3.insert(document);
    */
    //Scanner in=new Scanner(System.in);
    //String tst;
    //System.out.println("Enter serach query:");
    //tst=in.nextLine();

    Pattern namesRegex;
    srchtest srch = new srchtest();
    srch.assignsearch("19/jan/2015");

    if (srch.result == "m3") {

        String tt = srch.month;

        // String tst = Character.toUpperCase(tt.charAt(0)) + tt.substring(1);

        String tst = "Aditya";
        //String tst1 = Character.toUpperCase(tst.charAt(0)) + tst.substring(1);
        namesRegex = Pattern.compile("^(.*)job(.*)you(.*)");

        prnt(namesRegex, coll3);

        BasicDBObject query = new BasicDBObject("subject", namesRegex);

        DBCursor cursor = coll3.find();
        System.out.println("***** Output *****");
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    }

    /*
     BasicDBObject abc=new BasicDBObject();
     abc.put("from","Myntra");
             
     DBCursor cursor = coll3.find(abc);
            
     while (cursor.hasNext())
    { 
              
    System.out.println(cursor.next()); 
               
    }
      */

    /*coll3.ensureIndex(new BasicDBObject("from", 1),new BasicDBObject("date", 1));
    List <DBObject> list = coll3.getIndexInfo();
            
    for (DBObject o : list) {
    System.out.println(o);
    }
         */
    //BasicDBObject abc=new BasicDBObject();
    //abc.put("subject","Internshala Campus Ambassador 3.0");
    //"2015-01-21T23:41:40.000Z"

    //DBCursor cursor = coll3.find(abc);
    //DBCursor.explain(BasicDB);
    //while(cursor.hasNext()) {
    //System.out.println(cursor.next());
    //}

}

From source file:com.softlyinspired.jlw.concerns.concernSet.java

License:Open Source License

public String[][] listall() {
    String concernText = new String();
    String concernList[][] = new String[100][4];

    try {/*from  w w w . j av  a2s  .c  om*/

        DBCollection coll = repoConnection.getConcernCollection();
        DBObject doc;

        String[] scriptHeader = new String[3];
        try {
            DBCursor allConcerns = coll.find();
            concernCount = allConcerns.count();

            for (int i = 0; i < (concernCount); i++) {
                doc = allConcerns.next();
                scriptHeader = getDetails(doc);
                concernList[i][0] = scriptHeader[0];
                concernList[i][1] = scriptHeader[1];
                concernList[i][2] = scriptHeader[2];
                concernList[i][3] = scriptHeader[3];
            }

        } catch (Exception e) {
            concernText = "No concerns have yet been defined " + coll.getFullName();
            JOptionPane.showMessageDialog(null, concernText, "Error", JOptionPane.PLAIN_MESSAGE);
        }

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.PLAIN_MESSAGE);

    }
    return concernList;

}

From source file:com.softlyinspired.jlw.connections.ConnectionSelector.java

License:Open Source License

private ArrayList<dbConnection> allConnections() {
    int numberFound = 0;
    ArrayList<dbConnection> connectionList = new ArrayList<dbConnection>();
    try {// www  . j a  v  a2 s  .  c  o  m

        DBCollection coll = repoConnection.getConnectionCollection();
        DBObject doc;

        try {
            DBCursor allConnections = coll.find();
            numberFound = allConnections.count();

            for (int i = 0; i < (numberFound); i++) {
                doc = allConnections.next();
                dbConnection a = new dbConnection();
                a.connectionName = doc.get("name").toString();
                connectionList.add(a);
            }
        } catch (Exception e) {
            JLWUtilities.scriptErrorMessage("Error finding connection");
        }
    } catch (Exception e) {
        JLWUtilities.scriptErrorMessage("General Error in listing");
    }

    return connectionList;
}

From source file:com.softlyinspired.jlw.mongodb.IdManager.java

License:Open Source License

public int getNextId(String collectionType) {
    String n = new String();
    DBCollection coll;
    String collkey = new String();

    switch (collectionType) {
    case "concern":
        coll = repoConnection.getConcernCollection();
        collkey = "concernId";
        break;//from www  . ja  v  a 2 s . c  o m
    case "script":
        coll = repoConnection.getScriptCollection();
        collkey = "scriptId";
        break;
    case "connection":
        coll = repoConnection.getConnectionCollection();
        collkey = "connectionId";
        break;
    case "menu":
        coll = repoConnection.getCustomMenusCollection();
        collkey = "scriptId";
        break;
    case "report":
        coll = repoConnection.getReportsCollection();
        collkey = "reportId";
        break;
    default:
        coll = null;
        break;
    }

    DBObject sort = new BasicDBObject();
    // -1 sorts descending
    sort.put(collkey, -1);

    DBCursor obj = coll.find().sort(sort).limit(1);

    System.out.println(obj.count());
    if (obj.count() > 0) {
        DBObject doc;
        doc = obj.next();
        n = doc.get(collkey).toString();
        return Integer.parseInt(n);
    } else {
        return 0;
    }

}

From source file:com.spring.tutorial.controllers.DefaultController.java

@RequestMapping(value = "/my-drive/search", method = RequestMethod.GET)
public @ResponseBody List<FOUFile> getSearchedFile(HttpServletRequest request)
        throws UnknownHostException, Exception {

    String tag = request.getParameter("tag");
    HttpSession session = request.getSession();
    List<FOUFile> files = new ArrayList<>();

    String title = request.getParameter("url");
    if (title.equals("my-drive")) {
        MongoData mongoData = new MongoData();
        DB db = mongoData.getDB();//from   ww w  . ja  v a  2 s  . c  o m
        DBCollection collection = db.getCollection(session.getAttribute("id") + "_files_meta");
        DBCursor cursor = collection.find();

        while (cursor.hasNext()) {
            DBObject document = cursor.next();
            String[] tags = document.get("tags").toString().split(" ,");
            if (document.get("name").toString().toLowerCase().contains(tag.toLowerCase())
                    || tag.equals("all")) {
                files.add(new MongoFile(document));
            } else {
                for (String oneTag : tags) {
                    String[] inputTags = tag.split(" ");
                    for (String inputTag : inputTags) {
                        if (oneTag.trim().contains(inputTag)) {
                            files.add(new MongoFile(document));
                            break;
                        }
                    }
                }
            }
        }
        return files;
    } else {
        MongoData mongoData = new MongoData();
        DB db = mongoData.getDB();
        DBCollection collection = db.getCollection(session.getAttribute("id") + "_dropbox_files_meta");
        DBCursor cursor = collection.find();

        while (cursor.hasNext()) {
            DBObject document = cursor.next();
            //  String[] tags = document.get("tags").toString().split(" ,");
            if (document.get("name").toString().toLowerCase().contains(tag.toLowerCase())
                    || tag.equals("all")) {
                files.add(new DropboxEntity(document));
            } /* else {
              for (String oneTag : tags) {
              String[] inputTags = tag.split(" ");
              for (String inputTag : inputTags) {
              if (oneTag.trim().contains(inputTag)) {
              files.add(new MongoFile(document));
              break;
              }
              }
              }
                          
              }*/

        }
        return files;
    }
}

From source file:com.spring.tutorial.controllers.DefaultController.java

@RequestMapping(value = "/my-drive/dropbox/get-info", method = RequestMethod.GET)
public @ResponseBody List<String> getDropboxFileInfo(HttpServletRequest request)
        throws IOException, DbxException, Exception {

    String path = request.getParameter("path");

    List<String> result = new ArrayList<>();

    MongoData mongoData = new MongoData();
    DB db = mongoData.getDB();//from w  ww  . jav a2 s .c o m
    DBCollection collection = db.getCollection(request.getSession().getAttribute("id") + "_dropbox_files_meta");

    DBCursor cursor = collection.find();

    while (cursor.hasNext()) {
        DBObject document = cursor.next();
        if (document.get("path").equals(path)) {
            result.add((String) document.get("tags"));
            result.add((String) document.get("description"));
        }
    }
    return result;
}

From source file:com.spring.tutorial.controllers.DefaultController.java

@RequestMapping(value = "/check-user-existence", method = RequestMethod.GET)
public @ResponseBody boolean checkIfUserExists(HttpServletRequest request)
        throws IOException, DbxException, Exception {
    String username = (String) request.getParameter("username");
    MongoData mongoData = new MongoData();
    DB db = mongoData.getDB();/*  w w w  . j a v a  2 s . co m*/
    DBCollection collection = db.getCollection("users");
    DBCursor cursor = collection.find();

    while (cursor.hasNext()) {
        DBObject doc = (DBObject) cursor.next();
        String retUsername = (String) doc.get("username");
        if (retUsername.equals(username))
            return true;
    }
    return false;
}

From source file:com.spring.tutorial.dropbox.DropBoxAuth.java

public void removeJunk(String id, DbxClient client, String path) throws UnknownHostException, Exception {

    List<String> paths = new ArrayList<>();
    getDropboxPaths(id, paths, "/");

    MongoData mongoData = new MongoData();
    DB db = mongoData.getDB();/*from   w w  w  .j a va 2  s  .  c  o  m*/
    DBCollection collection = db.getCollection(id + "_dropbox_files_meta");
    DBCursor cursor = collection.find();

    if (cursor.count() != 0) {
        while (cursor.hasNext()) {
            boolean found = false;
            DBObject doc = cursor.next();
            for (String onePath : paths) {
                if (doc.get("path").equals(onePath)) {
                    found = true;
                }
            }
            if (found == false) {
                collection.remove(doc);
            }
        }
    }
}

From source file:com.spring.tutorial.dropbox.DropBoxAuth.java

public void fetchFilesAndFolders(String id, DbxClient client, String path)
        throws DbxException, UnknownHostException, Exception {

    MongoData mongoData = new MongoData();
    DB db = mongoData.getDB();//from  w w w .j  a va 2s  . c om
    DBCollection collection = db.getCollection(id + "_dropbox_files_meta");
    DBCursor cursor = collection.find();

    DbxEntry.WithChildren listing = client.getMetadataWithChildren(path);

    for (DbxEntry child : listing.children) {
        String fileInfo = child.toString();
        DropboxEntity entity;
        if (child.isFile()) {
            String rev = fileInfo.substring(fileInfo.indexOf("rev=") + 5, fileInfo.indexOf(")") - 1);
            String fileSize = fileInfo.substring(fileInfo.indexOf("humanSize=") + 11,
                    fileInfo.indexOf("\", lastModified"));
            String fileLastModified = fileInfo.substring(fileInfo.indexOf("lastModified=") + 14,
                    fileInfo.indexOf("\", clientMtime"));

            entity = new DropboxEntity(child.name, "file", fileSize, fileLastModified, child.path, rev);

            DBObject document = new BasicDBObject();
            document.put("rev", rev);
            document.put("fileSize", fileSize);
            document.put("lastModified", fileLastModified);
            document.put("name", child.name);
            document.put("path", child.path);
            document.put("type", "file");

            cursor = collection.find();
            boolean found = false;

            while (cursor.hasNext()) {
                DBObject doc = cursor.next();
                String path1 = (String) doc.get("path");
                if (path1.equals(child.path)) {
                    found = true;
                }
            }

            if (found) {
                document.put("found", "true");
                BasicDBObject newDocument = new BasicDBObject();
                newDocument.append("$set", document);
                BasicDBObject searchQuery = new BasicDBObject().append("path", child.path);
                collection.update(searchQuery, newDocument);
            } else {
                document.put("tags", "");
                document.put("description", "");
                document.put("found", "false");
                collection.insert(document);
            }

        } else {
            entity = new DropboxEntity(child.name, "folder", child.path);

            DBObject document = new BasicDBObject();
            document.put("rev", "");
            document.put("fileSize", "");
            document.put("lastModified", "");
            document.put("name", child.name);
            document.put("path", child.path);
            document.put("type", "folder");

            cursor = collection.find();
            boolean found = false;
            if (cursor.hasNext()) {
                while (cursor.hasNext()) {
                    DBObject doc = cursor.next();
                    String path1 = (String) doc.get("path");
                    if (path1.equals(child.path)) {
                        found = true;
                    }
                }
            }

            if (found) {
                document.put("found", "true");
                BasicDBObject newDocument = new BasicDBObject();
                newDocument.append("$set", document);
                BasicDBObject searchQuery = new BasicDBObject().append("path", child.path);
                collection.update(searchQuery, newDocument);
            } else {
                document.put("tags", "");
                document.put("description", "");
                document.put("found", "false");
                collection.insert(document);
            }

            fetchFilesAndFolders(id, client, child.path);
        }
    }
}

From source file:com.spring.tutorial.dropbox.DropboxEntityFactory.java

public List<DropboxEntity> getFilesAndFolders(String id, String path) throws UnknownHostException, Exception {
    MongoData mongoData = new MongoData();
    DB db = mongoData.getDB();//  ww w. jav a2 s. c  o m
    DBCollection collection = db.getCollection(id + "_dropbox_files_meta");
    DBCursor cursor = collection.find();

    while (cursor.hasNext()) {
        DBObject doc = cursor.next();
        String docPath = doc.get("path").toString();
        String docPathHead = docPath.substring(docPath.indexOf("/"), docPath.lastIndexOf("/") + 1);

        if (docPathHead.equals(path)) {
            DropboxEntity entity = new DropboxEntity(doc);
            dropboxFiles.add(entity);
        }
    }
    return dropboxFiles;
}