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(final DBObject query) 

Source Link

Document

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

Usage

From source file:com.impetus.kundera.client.MongoDBClient.java

License:Apache License

@Override
public <E> E loadColumns(EntityManagerImpl em, Class<E> clazz, String dbName, String documentName, String key,
        EntityMetadata m) throws Exception {
    log.debug("Fetching data from " + documentName + " for PK " + key);
    DBCollection dbCollection = mongoDb.getCollection(documentName);

    BasicDBObject query = new BasicDBObject();
    query.put(m.getIdColumn().getName(), key);

    DBCursor cursor = dbCollection.find(query);
    DBObject fetchedDocument = null;/*from w  w  w . j a  v  a2  s . co m*/

    if (cursor.hasNext()) {
        fetchedDocument = cursor.next();
    } else {
        return null;
    }

    Object entity = new MongoDBDataHandler().getEntityFromDocument(em, clazz, m, fetchedDocument);

    return (E) entity;
}

From source file:com.impetus.kundera.client.MongoDBClient.java

License:Apache License

@Override
public <E> List<E> loadColumns(EntityManagerImpl em, Class<E> clazz, String dbName, String documentName,
        EntityMetadata m, String... keys) throws Exception {
    log.debug("Fetching data from " + documentName + " for Keys " + keys);

    DBCollection dbCollection = mongoDb.getCollection(documentName);

    BasicDBObject query = new BasicDBObject();
    query.put(m.getIdColumn().getName(), new BasicDBObject("$in", keys));

    DBCursor cursor = dbCollection.find(query);

    List entities = new ArrayList<E>();
    while (cursor.hasNext()) {
        DBObject fetchedDocument = cursor.next();
        Object entity = new MongoDBDataHandler().getEntityFromDocument(em, clazz, m, fetchedDocument);
        entities.add(entity);/*  w  w  w .  j a  v a2s .c  om*/
    }
    return entities;
}

From source file:com.impetus.kundera.client.MongoDBClient.java

License:Apache License

@Override
public void delete(String idColumnName, String documentName, String rowId) throws Exception {
    DBCollection dbCollection = mongoDb.getCollection(documentName);

    //Find the DBObject to remove first
    BasicDBObject query = new BasicDBObject();
    query.put(idColumnName, rowId);/*  w  ww  . j av  a2s .  c  om*/

    DBCursor cursor = dbCollection.find(query);
    DBObject documentToRemove = null;

    if (cursor.hasNext()) {
        documentToRemove = cursor.next();
    } else {
        throw new PersistenceException(
                "Can't remove Row# " + rowId + " for " + documentName + " because record doesn't exist.");
    }

    dbCollection.remove(documentToRemove);
}

From source file:com.janeluo.jfinalplus.plugin.monogodb.MongoKit.java

License:Apache License

public static Page<Record> paginate(String collection, int pageNumber, int pageSize, Map<String, Object> filter,
        Map<String, Object> like, Map<String, Object> sort) {
    DBCollection logs = MongoKit.getCollection(collection);
    BasicDBObject conditons = new BasicDBObject();
    buildFilter(filter, conditons);//from   w  w w. j  a v  a  2  s . c  om
    buildLike(like, conditons);
    DBCursor dbCursor = logs.find(conditons);
    page(pageNumber, pageSize, dbCursor);
    sort(sort, dbCursor);
    List<Record> records = new ArrayList<Record>();
    while (dbCursor.hasNext()) {
        records.add(toRecord(dbCursor.next()));
    }
    int totalRow = dbCursor.count();
    if (totalRow <= 0) {
        return new Page<Record>(new ArrayList<Record>(0), pageNumber, pageSize, 0, 0);
    }
    int totalPage = totalRow / pageSize;
    if (totalRow % pageSize != 0) {
        totalPage++;
    }
    Page<Record> page = new Page<Record>(records, pageNumber, pageSize, totalPage, totalRow);
    return page;
}

From source file:com.javamongodb.application.RecordsActivity.java

private void update(String columnName, String newValue, String mobileNumber) {
    try {//from w w  w. j a  v a2s .co  m
        DBCollection collection = DatabaseUtils.openDBConnection();
        BasicDBObject query = new BasicDBObject("MobileNumber", new BasicDBObject("$regex", mobileNumber));
        DBCursor cursor = collection.find(query);
        DBObject doc = cursor.next();
        Object id = doc.get("_id");
        Object prevData = doc.get(columnName);
        if (!(newValue.equals(prevData.toString()))) {
            BasicDBObject updateDocument = new BasicDBObject();
            updateDocument.append("$set", new BasicDBObject().append(columnName, newValue));
            BasicDBObject searchQuery = new BasicDBObject().append("_id", id);
            int i = JOptionPane.showConfirmDialog(null, messages.getString("question.to.save"),
                    messages.getString("title.save.confirm"), JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE);
            if (i == JOptionPane.YES_OPTION) {
                collection.update(searchQuery, updateDocument); //update the document
                JOptionPane.showMessageDialog(null, messages.getString("notification.database.updated"),
                        messages.getString("title.update.confirm"), JOptionPane.INFORMATION_MESSAGE);
            }
        }
        logger.debug("Updating object with id: " + id + " succeeded");
    } catch (Exception exception) {
        logger.error("Updating object failed:\n" + exception.getMessage());
    } finally {
        logger.debug("Closing database connection");
        DatabaseUtils.closeDBConnection();
    }
}

From source file:com.jhkt.playgroundArena.shared.tasks.MongoDBTaskRunner.java

License:Apache License

public List<AbstractDocument> query(AbstractDocument queryCriteria) {
    DBCollection collection = getDBCollection(queryCriteria);
    DBCursor cursor = collection.find(queryCriteria.resolveToBasicDBObject());
    List<AbstractDocument> queryResult = new LinkedList<AbstractDocument>();

    while (cursor.hasNext()) {
        DBObject result = cursor.next();
        queryResult.add(AbstractDocument.convertBacktoClassInstance(result));
    }/* ww  w  . ja va 2s .  c  o  m*/

    return queryResult;
}

From source file:com.jjorgemoura.hangmanz.model.ZDHangmanGame.java

public static ZDHangmanGame loadByUUID(String uuid, DB dbEngine) {

    ZDHangmanGame theGame = null;//from   w  w w  . jav  a 2 s.c o m

    if (dbEngine == null) {

        return null;
    }

    //Prepare Collection
    DBCollection dbCollection = dbEngine.getCollection(ZDHangmanGame.DB_COLLECTION_ID);

    BasicDBObject query = new BasicDBObject();
    query.put("hm_uuid", uuid);
    DBCursor cursor = dbCollection.find(query);

    while (cursor.hasNext()) {

        DBObject x = cursor.next();
        //System.out.println(x);

        theGame = new ZDHangmanGame(x, dbEngine);
    }

    return theGame;
}

From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoRowLocalServiceImpl.java

License:Open Source License

protected List<ExpandoRow> getRows(ExpandoTable expandoTable, int start, int end) {

    DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable);

    DBCursor dbCursor = dbCollection.find(new BasicDBObject());

    if ((start != QueryUtil.ALL_POS) && (end != QueryUtil.ALL_POS)) {
        dbCursor.limit(start);/*from w  w  w . ja  va2s  .c  o  m*/
        dbCursor.limit(end - start);
    }

    List<ExpandoRow> expandoRows = new ArrayList<ExpandoRow>();

    for (DBObject expandoRowDBObject : dbCursor.toArray()) {
        ExpandoRow expandoRow = toExpandoRow(expandoRowDBObject, expandoTable);

        expandoRows.add(expandoRow);
    }

    return expandoRows;
}

From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java

License:Open Source License

@Override
public List<ExpandoValue> getColumnValues(long companyId, long classNameId, String tableName, String columnName,
        String data, int start, int end) {

    try {//w w w .j a  va 2 s .  co  m
        ExpandoColumn expandoColumn = ExpandoColumnLocalServiceUtil.getColumn(companyId, classNameId, tableName,
                columnName);

        DBCollection dbCollection = MongoDBUtil.getCollection(companyId, classNameId, tableName);

        DBCursor dbCursor = null;

        if (Validator.isNotNull(data)) {
            DBObject queryDBObject = new BasicDBObject();

            ExpandoValue expandoValue = ExpandoValueUtil.create(0);

            expandoValue.setColumnId(expandoColumn.getColumnId());
            expandoValue.setData(data);

            queryDBObject.put(columnName, getData(expandoColumn, expandoValue));

            dbCursor = dbCollection.find(queryDBObject);
        } else {
            dbCursor = dbCollection.find();
        }

        if ((start != QueryUtil.ALL_POS) && (end != QueryUtil.ALL_POS)) {
            dbCursor = dbCursor.skip(start).limit(end - start);
        }

        List<ExpandoValue> expandoValues = new ArrayList<ExpandoValue>();

        for (DBObject dbObject : dbCursor.toArray()) {
            BasicDBObject expandoValueDBObject = (BasicDBObject) dbObject;

            ExpandoValue expandoValue = toExpandoValue(expandoValueDBObject, expandoColumn);

            expandoValues.add(expandoValue);
        }

        return expandoValues;
    } catch (PortalException pe) {
        throw new SystemException(pe);
    }
}

From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java

License:Open Source License

@Override
public int getColumnValuesCount(long companyId, long classNameId, String tableName, String columnName,
        String data) {//from ww w  .j a v  a2  s  .c om

    try {
        ExpandoColumn expandoColumn = ExpandoColumnLocalServiceUtil.getColumn(companyId, classNameId, tableName,
                columnName);

        DBCollection dbCollection = MongoDBUtil.getCollection(companyId, classNameId, tableName);

        DBCursor dbCursor = null;

        if (Validator.isNotNull(data)) {
            DBObject queryDBObject = new BasicDBObject();

            ExpandoValue expandoValue = ExpandoValueUtil.create(0);

            expandoValue.setColumnId(expandoColumn.getColumnId());
            expandoValue.setData(data);

            queryDBObject.put(columnName, getData(expandoColumn, expandoValue));

            dbCursor = dbCollection.find(queryDBObject);
        } else {
            dbCursor = dbCollection.find();
        }

        return dbCursor.count();
    } catch (PortalException pe) {
        throw new SystemException(pe);
    }
}