Example usage for com.mongodb GroupCommand GroupCommand

List of usage examples for com.mongodb GroupCommand GroupCommand

Introduction

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

Prototype

public GroupCommand(final DBCollection collection, final String keyf, final DBObject condition,
        final DBObject initial, final String reduce, final String finalize) 

Source Link

Document

Creates a new group command.

Usage

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void group(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    DBObject keys = ((DocBuilderField) getBoundUnit(Item.grpKeys)).getDBObject();
    DBObject initial = ((DocBuilderField) getBoundUnit(Item.grpInitialValue)).getDBObject();
    DBObject query = ((DocBuilderField) getBoundUnit(Item.grpQuery)).getDBObject();
    String reduce = getStringFieldValue(Item.grpReduce);
    String finalize = getStringFieldValue(Item.grpFinalize);
    final GroupCommand cmd = new GroupCommand(col, keys, query, initial, reduce, finalize);
    //        new DocView(null, "Group", col.getDB(), cmd.toDBObject()).addToTabbedDiv();

    new DbJobCmd(col.getDB(), cmd.toDBObject(), null, button).addJob();
}

From source file:com.zuehlke.sbdfx.dataaccess.mongo.MongoCitiesDao.java

@Override
public BoundingBox getGlobalBoundingBox() {

    // example taken from: http://stackoverflow.com/questions/4762980/getting-the-highest-value-of-a-column-in-mongodb

    DBObject keys = null;/*  w  ww  .  jav a2  s .c o  m*/
    DBObject condition = null;
    DBObject initial = new BasicDBObject();
    initial.put("maxLongitude", Double.MIN_VALUE);
    initial.put("maxLatitude", Double.MIN_VALUE);
    initial.put("minLongitude", Double.MAX_VALUE);
    initial.put("minLatitude", Double.MAX_VALUE);

    String reduce = "function(obj,prev) { "
            + "if ( prev.maxLongitude < obj.longitude ) prev.maxLongitude = obj.longitude; "
            + "if ( prev.minLongitude > obj.longitude ) prev.minLongitude = obj.longitude; "
            + "if ( prev.maxLatitude < obj.latitude ) prev.maxLatitude = obj.latitude; "
            + "if ( prev.minLatitude > obj.latitude ) prev.minLatitude = obj.latitude; " + "}";
    String finalize = null;

    GroupCommand cmd = new GroupCommand(cities, keys, condition, initial, reduce, finalize);
    DBObject resultObjectList = cities.group(cmd);

    BoundingBox result = new BoundingBox();
    DBObject resultObject = (DBObject) resultObjectList.get("0");
    result.setLongitudeMax((double) resultObject.get("maxLongitude"));
    result.setLongitudeMin((double) resultObject.get("minLongitude"));
    result.setLatitudeMax((double) resultObject.get("maxLatitude"));
    result.setLatitudeMin((double) resultObject.get("minLatitude"));
    return result;
}

From source file:govt_import_export.export.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    // TODO add your handling code here:
    MongoClient mongo = null;/*from  w w  w  . j a  v  a2s. c om*/
    try {
        mongo = new MongoClient("localhost", 27017);
        //get database
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    catch (MongoException e) {
        e.printStackTrace();
    }
    DB db = mongo.getDB("AUTOMOBILEXPO");
    //DB db2=mongo.getDB("automobile_expo");
    String batches = (String) jComboBox1.getSelectedItem();
    int unit, month, year;

    unit = Integer.parseInt(batches);
    //get collections
    DBCollection table = db.getCollection(manufac);
    //DBCollection table1=db2.getCollection(db1);
    BasicDBObject document1 = new BasicDBObject();
    String st = (String) jComboBox2.getSelectedItem();
    String st1 = (String) jComboBox3.getSelectedItem();
    String st2 = (String) jComboBox4.getSelectedItem();
    month = Integer.parseInt(st1);
    year = Integer.parseInt(st2);

    cost = cost * unit;
    //jTextField1.setText(z);
    DB db0 = mongo.getDB("admindata");
    DBCollection collection0 = db0.getCollection("admin");
    DBCursor c = collection0.find(); //yes            
    DBObject o;
    //DBCursor c=collection0.find(); //yes  
    while (c.hasNext()) {
        o = c.next();
        if (o.get("rate") != null) {
            db_rate = o.get("rate").toString();
        }
    }

    c = collection0.find();

    while (c.hasNext()) {
        o = c.next();
        if (o.get("balance") != null) {
            db_balance = nf.format(o.get("balance"));

        }
    }

    ratio = ((double) cost) / Double.parseDouble(db_balance);
    ratio = -ratio * .1;
    newbalance = Long.parseLong(db_balance) - cost;
    newrate = Double.parseDouble(db_rate) * pow(Math.E, ratio);
    System.out.println("'newrate'" + newrate);
    //System.out.println(newbalance);
    String q = nf.format(newbalance);
    //System.out.println(q);

    //--------------------setting the updated values in the db----------------------

    BasicDBObject searchupdate = new BasicDBObject();
    searchupdate.append("rate", Double.parseDouble(db_rate)); //finds the document(s) where "date":last_activity

    BasicDBObject update = new BasicDBObject();
    update.append("$set", new BasicDBObject("rate", /*Double.toString(newrate)*/newrate));

    collection0.update(searchupdate, update);

    //-----
    BasicDBObject searchupdate1 = new BasicDBObject();
    searchupdate1.append("balance", Long.parseLong(db_balance));

    BasicDBObject update1 = new BasicDBObject();
    update1.append("$set", new BasicDBObject("balance", Long.parseLong(q)));

    collection0.update(searchupdate1, update1);

    //DBObject o;
    BasicDBObject document = new BasicDBObject();
    BasicDBObject prev = new BasicDBObject();
    document.append("manufacturer", manufac);
    document.append("model", model_selected);
    document.append("country", st);
    document.append("Units", unit);
    document.append("Cost", cost);

    document.append("Month", month);
    document.append("Year", year);
    table.insert(document);
    //long x=table.count();
    //find and display
    //System.out.println(x);
    GroupCommand cmd = new GroupCommand(table, null, new BasicDBObject("model", model_selected),
            new BasicDBObject("Units", 0), "function(document,prev){prev.Units=prev.Units+this.Units;}", null);
    o = table.group(cmd);
    //System.out.println(o);
    sort_cars();
    javax.swing.JOptionPane.showMessageDialog(export.this, "Waiting for acceptance");
    st = String.valueOf(total_cost);
    jTextField1.setText(st);
    System.out.println("donee");
}

From source file:net.vz.mongodb.jackson.JacksonDBCollection.java

License:Apache License

/**
 * Applies a group operation//from ww w  .j a  v a 2s . c o m
 *
 * @param key      - { a : true }
 * @param cond     - optional condition on query
 * @param reduce   javascript reduce function
 * @param initial  initial value for first match on a key
 * @param finalize An optional function that can operate on the result(s) of the reduce function.
 * @return The results
 * @throws MongoException If an error occurred
 * @see <a href="http://www.mongodb.org/display/DOCS/Aggregation">http://www.mongodb.org/display/DOCS/Aggregation</a>
 */
public DBObject group(DBObject key, DBObject cond, DBObject initial, String reduce, String finalize)
        throws MongoException {
    GroupCommand cmd = new GroupCommand(dbCollection, key, cond, initial, reduce, finalize);
    return group(cmd);
}

From source file:org.exist.mongodb.xquery.mongodb.collection.Group.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {/*from   w  ww  .j  av a  2 s .c o m*/
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        BasicDBObject key = (BasicDBObject) JSON.parse(args[3].itemAt(0).getStringValue());

        BasicDBObject condition = (BasicDBObject) JSON.parse(args[4].itemAt(0).getStringValue());

        BasicDBObject initial = (BasicDBObject) JSON.parse(args[5].itemAt(0).getStringValue());

        String reduce = args[6].itemAt(0).getStringValue();

        // The finalize can be null
        String finalize = (args.length >= 8) ? args[7].itemAt(0).getStringValue() : null;

        // Get database
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        // Propare groupcommand
        GroupCommand command = new GroupCommand(dbcol, key, condition, initial, reduce, finalize);

        System.out.println(command.toDBObject().toString());

        if (LOG.isDebugEnabled()) {
            LOG.debug(command.toDBObject().toString());
        }

        // Execute, finalize can have value null
        DBObject result = dbcol.group(command);

        // Execute query
        Sequence retVal = new StringValue(result.toString());

        return retVal;

    } catch (JSONParseException ex) {
        LOG.error(ex.getMessage());
        throw new XPathException(this, MongodbModule.MONG0004, ex.getMessage());

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0003, ex.getMessage());
    }

}