Example usage for com.mongodb DBCursor next

List of usage examples for com.mongodb DBCursor next

Introduction

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

Prototype

@Override
public DBObject next() 

Source Link

Document

Returns the object the cursor is at and moves the cursor ahead by one.

Usage

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * Mongo ID generation like it is done in the grails gorm framework
 *
 * @param collectionName// w w w .  j  a va 2 s .com
 *            The name of the collection the id should be generated for
 * @param db
 *            The mongodb connection
 * @return a new id
 */
private Long generateIdentifier(String collectionName, DB db) {

    // get or create the Collection for the ID storage
    DBCollection dbCollection = db.getCollection(collectionName + ".next_id");
    // create entry to store the newly generated id
    DBObject nativeEntry = new BasicDBObject();

    while (true) {
        DBCursor result = dbCollection.find().sort(new BasicDBObject("_id", -1)).limit(1);

        long nextId;
        if (result.hasNext()) {
            final Long current = (Long) result.next().get("_id");
            nextId = current + 1;
        } else {
            nextId = 1;
        }

        nativeEntry.put("_id", nextId);
        final WriteResult writeResult = dbCollection.insert(nativeEntry);
        final CommandResult lastError = writeResult.getLastError();
        if (lastError.ok()) {
            break;
        }

        final Object code = lastError.get("code");
        // duplicate key error try again
        if (code != null && code.equals(11000)) {
            continue;
        }
        break;
    }

    return (Long) nativeEntry.get("_id");
}

From source file:com.espirit.moddev.examples.uxbridge.newswidget.mongodb.ArticleHandler.java

License:Apache License

/**
 * loads an article by the document id/* w ww .j ava  2 s . c om*/
 *
 * @param id       the id of the document
 * @param language language abbreviation like EN or DE
 * @return the document or null
 */
private DBObject getById(Long id, String language) {
    BasicDBObject query = new BasicDBObject();
    query.put("aid", id);
    query.put("language", language);

    DBCursor cur = articles.find(query);

    if (cur.hasNext()) {
        return cur.next();
    }
    return null;
}

From source file:com.ewcms.mongo.demo.repositories.PersonRepositoryImpl.java

License:Open Source License

@Override
public List<Person> findWorkByAgeRang(final int start, final int end) {
    return getMongoOperations().execute(new DbCallback<List<Person>>() {
        @Override/*from  w ww.  j av a  2 s . co  m*/
        public List<Person> doInDB(DB db) throws MongoException, DataAccessException {
            DBCollection coll = db.getCollection("person");
            BasicDBObject query = new BasicDBObject();
            query.put("age", new BasicDBObject("$gte", 20).append("$lte", 60));
            DBCursor cur = coll.find(query);
            List<Person> list = new ArrayList<Person>();
            for (; cur.hasNext();) {
                DBObject source = cur.next();
                Person person = convertEntity(source);
                list.add(person);
            }
            return list;
        }
    });
}

From source file:com.example.rest.DbConnection.java

public User getData(int number) {
    BasicDBObject searchQuery = new BasicDBObject();
    User retUser = null;/*from   ww  w. j a  v  a 2s  .c o m*/

    searchQuery.append("DriverLicense", number);

    DBCursor cursor = table.find(searchQuery);

    while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        retUser = new User((int) obj.get("DriverLicense"));
        retUser.setFirstName((String) obj.get("FirstName"));
        retUser.setLastName((String) obj.get("LastName"));
        retUser.setDob((String) obj.get("DOB"));
        retUser.setAddress((String) obj.get("Address"));
        //retUser = (User) cursor.next();
    }
    return retUser;
}

From source file:com.example.rest.DbConnection.java

public List<User> allCustomer() {
    BasicDBObject query = new BasicDBObject();
    DBCursor cursor = table.find();
    List<User> all = new ArrayList<User>();
    User retUser = null;/*from w w  w .  j a va 2  s.c o  m*/
    while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        retUser = new User((int) obj.get("DriverLicense"));
        retUser.setFirstName((String) obj.get("FirstName"));
        retUser.setLastName((String) obj.get("LastName"));
        retUser.setDob((String) obj.get("DOB"));
        retUser.setAddress((String) obj.get("Address"));
        all.add(retUser);
    }
    return all;
}

From source file:com.example.rest.DbConnection.java

public String getOrder(int number) {
    BasicDBObject document = new BasicDBObject();
    String order = null;/*from   ww  w .j  a va  2s .  co m*/

    document.append("DriverLicense", number);
    DBCursor cursor = orderTable.find(document);
    if (cursor.hasNext()) {
        DBObject obj = cursor.next();
        order = (String) obj.get("OrderDetails");
    }
    return order;
}

From source file:com.ff.reportgenerator.mongodb.DynamicDatabase.java

public String query() {
    String records = "<table>" + Utility.formTableHead() + "<tbody>";

    DB myDB = getDB(DB_NAME);/*from   w  w w . jav  a  2s  .c o  m*/
    DBCollection coll = myDB.getCollection("projects");

    DBCursor ret = coll.find();
    BasicDBObject sort = new BasicDBObject("PROJECT_ID", 1);
    ret.sort(sort);

    try {
        while (ret.hasNext()) {
            records = records + "<tr>";

            DBObject rec = (DBObject) ret.next();
            Iterator keys = Utility.DATA_KEYS.iterator();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                String value = (String) rec.get(key);
                if (key.equals("PROJECT_ID")) {
                    records = records + "<td><a href=\"" + Utility.PROJECT_URL_PREFIX + value + "\">" + value
                            + "</td>";
                } else {
                    records = records + "<td>" + value + "</td>";

                }
            }

            records = records + "</tr>";
            //System.out.println(rec);
        }
    } finally {
        ret.close();
    }

    records = records + "</tbody></table>";
    return records;
}

From source file:com.fileoperations.CopyClass.java

public CopyClass(String userID, String parentPath, String name, String newPath) throws UnknownHostException {
    this.userID = userID;
    this.parentPath = parentPath;
    this.name = name;
    this.newPath = newPath;
    mymongo = new Connection();
    DBCollection usercol = mymongo.getMembersCol();
    BasicDBObject query = new BasicDBObject();
    query.put("_id", userID);
    DBCursor cursor = usercol.find(query);
    DBObject col = cursor.next();
    userCollectionName = col.get("collection").toString();
    collection = mymongo.getCollection(userCollectionName);
}

From source file:com.fileoperations.CopyClass.java

public Boolean forSingleFile() {
    try {//w  ww.j ava  2 s .com
        if (name.contains(".")) {

            BasicDBObject query = new BasicDBObject();
            query.put("_id", parentPath + pathMerger + name);
            DBCursor cursor = collection.find(query);

            if (cursor.hasNext()) {
                BasicDBObject checknewquery = new BasicDBObject();
                checknewquery.put("_id", newPath + pathMerger + name);
                DBCursor tempCursor = collection.find(checknewquery);
                if (tempCursor.hasNext()) {
                    return false;
                }
                DBObject copyFile = cursor.next();
                GridFS fileDB = new GridFS(mymongo.getDB(), userCollectionName);
                InputStream data = fileDB.findOne(query).getInputStream();

                BasicDBObject document = new BasicDBObject();
                document.append("_id", newPath + pathMerger + name);
                document.append("folder", "0");
                document.append("parent", newPath);
                document.append("name", name);
                document.append("type", copyFile.get("type").toString());
                collection.insert(document);
                GridFSInputFile inputFile = fileDB.createFile(data);
                inputFile.setId(newPath + pathMerger + name);
                inputFile.put("path", newPath);
                inputFile.setFilename(name);
                inputFile.save();
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } finally {
        mymongo.closeConnection();
    }
}

From source file:com.fileoperations.FolderDownload.java

public FolderDownload(String userID, String parentPath, String folderName) throws UnknownHostException {
    this.userID = userID;
    this.parentPath = parentPath;
    this.folderName = folderName;
    mymongo = new Connection();
    DBCollection usercol = mymongo.getMembersCol();
    BasicDBObject query = new BasicDBObject();
    query.put("_id", userID);
    DBCursor cursor = usercol.find(query);
    DBObject col = cursor.next();
    userCollectionName = col.get("collection").toString();
    collection = mymongo.getCollection(userCollectionName);
}