Example usage for com.mongodb DBCollection group

List of usage examples for com.mongodb DBCollection group

Introduction

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

Prototype

@Deprecated
public DBObject group(final DBObject key, final DBObject cond, final DBObject initial, final String reduce,
        @Nullable final String finalize) 

Source Link

Document

Group documents in a collection by the specified key and performs simple aggregation functions such as computing counts and sums.

Usage

From source file:com.hangum.tadpole.mongodb.core.editors.group.MongoDBGroupEditor.java

License:Open Source License

private void search() throws Exception {
    final DBCollection dbCol = MongoDBQuery.findCollection(userDB, initColName);

    final DBObject dbObjectKey = (DBObject) JSON.parse(textKeys.getText());
    final DBObject dbObjectInitial = (DBObject) JSON.parse(textInitialValue.getText());
    final DBObject dbObjectCondition = (DBObject) JSON.parse(textQuery.getText());

    final String strReductFunction = textReduceFunction.getText();
    final String strFinalize = textFinalizeFunction.getText();

    // search job
    Job job = new Job("Group Search job") { //$NON-NLS-1$
        @Override//from  w  w w .j  av a  2 s  . co m
        public IStatus run(IProgressMonitor monitor) {
            monitor.beginTask("Starting JSON query...", IProgressMonitor.UNKNOWN); //$NON-NLS-1$
            try {
                resultDBObject = dbCol.group(dbObjectKey, dbObjectCondition, dbObjectInitial, strReductFunction,
                        strFinalize);

            } catch (Exception e) {
                logger.error("Group exception", e); //$NON-NLS-1$
                return new Status(Status.WARNING, Activator.PLUGIN_ID, "Group " + e.getMessage()); //$NON-NLS-1$
            } finally {
                monitor.done();
            }

            return Status.OK_STATUS;
        }
    };

    // job? event  ?.
    job.addJobChangeListener(new JobChangeAdapter() {
        public void done(IJobChangeEvent event) {

            final IJobChangeEvent jobEvent = event;
            getSite().getShell().getDisplay().asyncExec(new Runnable() {
                public void run() {
                    if (jobEvent.getResult().isOK()) {
                        try {
                            compositeResult.refreshDBView(resultDBObject, 0);
                            compositeResult.setResult();
                        } catch (Exception e) {
                            logger.error("MapReduce Error", e); //$NON-NLS-1$
                            Status errStatus = new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(),
                                    e); //$NON-NLS-1$
                            ExceptionDetailsErrorDialog.openError(null, "Error", "MapReduce execute exception", //$NON-NLS-1$//$NON-NLS-2$
                                    errStatus);
                        }
                    } else {
                        //                     compositeResult.errorView(jobEvent.getResult().getMessage());
                    }
                }
            }); // end display.asyncExec            
        } // end done
    }); // end job

    job.setName(userDB.getDisplay_name());
    job.setUser(true);
    job.schedule();

}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestGroup.java

License:Open Source License

/**
 * @param args//from  www.  j a v a 2 s .com
 */
public static void main(String[] args) throws Exception {

    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");
    DBCollection coll = db.getCollection("group");

    DBObject dbObjectKey = (DBObject) JSON.parse(key);
    DBObject dbObjectInitial = (DBObject) JSON.parse(initial);
    DBObject dbObjectCondition = (DBObject) JSON.parse(condition);

    DBObject resultDBObject = coll.group(dbObjectKey, dbObjectCondition, dbObjectInitial, reduce, finalizer);
    System.out.println(resultDBObject);

    mongo.close();

    try {
        Thread.sleep(1);
    } catch (Exception e) {
    }
}

From source file:mongodb.javaGroup.java

public static void firstLetterTotals(DBCollection collection) {
    BasicDBObject key = new BasicDBObject("first", true);
    BasicDBObject cond = new BasicDBObject();
    BasicDBObject initial = new BasicDBObject("vowels", 0);
    initial.append("cons", 0);
    String reduce = "function (obj, prev) { " + "prev.vowels += obj.stats.vowels; "
            + "prev.cons += obj.stats.consonants; " + "}";
    String finalize = "function (obj) { " + "obj.total = obj.vowels + obj.cons; " + "}";
    DBObject result = collection.group(key, cond, initial, reduce, finalize);
    System.out.println("\nWords grouped by first letter " + "with totals: ");
    javaGroup.displayGroup(result);//from   w ww.  j ava  2s.  c o m
}

From source file:org.chililog.server.data.RepositoryEntryController.java

License:Apache License

/**
 * Count of number of entries that matches the condition
 * /*w w  w. ja va  2s . co  m*/
 * @param db
 *            Database connection
 * @param criteria
 *            Criteria to filter resultset. Fields, Conditions, Initial, ReduceFunction and FinalizeFunction are
 *            used.
 * @return Specified fields and aggregation counter.
 */
public DBObject executeGroupQuery(DB db, RepositoryEntryListCriteria criteria) throws ChiliLogException {
    try {
        if (db == null) {
            throw new NullArgumentException("db");
        }
        if (criteria == null) {
            throw new NullArgumentException("criteria");
        }

        DBCollection coll = db.getCollection(this.getDBCollectionName());

        DBObject fields = criteria.getFieldsDbObject();
        DBObject conditions = criteria.getConditionsDbObject();
        DBObject initial = criteria.getIntialDbObject();

        return coll.group(fields, conditions, initial, criteria.getReduceFunction(),
                criteria.getFinalizeFunction());
    } catch (Exception ex) {
        throw new ChiliLogException(ex, Strings.MONGODB_QUERY_ERROR, ex.getMessage());
    }
}