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 GroupCommand cmd) 

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: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;/*w  w  w.j a v  a  2s .  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:org.exist.mongodb.xquery.mongodb.collection.Group.java

License:Open Source License

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

    try {//w w  w.j av a 2s .  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());
    }

}