Example usage for com.mongodb BasicDBList BasicDBList

List of usage examples for com.mongodb BasicDBList BasicDBList

Introduction

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

Prototype

BasicDBList

Source Link

Usage

From source file:io.liveoak.mongo.MongoAggregationResource.java

License:Open Source License

private BasicDBList aggregate(RequestContext ctx) {
    BasicDBList queryObject = new BasicDBList();
    if (ctx.resourceParams() != null && ctx.resourceParams().contains("q")) {
        String queryString = ctx.resourceParams().value("q");
        DBObject paramObject = (DBObject) JSON.parse(queryString);

        if (paramObject instanceof BasicDBList) {
            queryObject = (BasicDBList) paramObject;
        } else {/*from  ww w  .j  a v  a 2 s.co  m*/
            queryObject.add(paramObject);
        }
    }

    DBCollection dbCollection = parent().getDBCollection();

    try {
        BasicDBList result = new BasicDBList();
        AggregationOutput output = dbCollection.aggregate((DBObject) queryObject.remove(0),
                queryObject.toArray(new DBObject[queryObject.size()]));
        for (DBObject dbObject : output.results()) {
            result.add(dbObject);
        }

        return result;

    } catch (Exception e) {
        logger().error("", e);
        throw new RuntimeException("Aggregation query failed: ", e);
    }
}

From source file:io.liveoak.mongo.MongoResource.java

License:Open Source License

protected BasicDBList createObjectList(Collection collection) throws Exception {
    BasicDBList dbList = new BasicDBList();
    for (Object object : collection) {
        if (object instanceof ResourceState) {
            dbList.add(createObject((ResourceState) object, true));
        } else if (object instanceof Collection) {
            dbList.add(createObjectList((Collection) object));
        } else {/*from  w w w .  j  a v  a2s.co m*/
            dbList.add(object);
        }
    }
    return dbList;
}

From source file:it.csi.smartdata.odata.datadiscovery.SDPExpressionVisitor.java

@Override
public Object visitBinary(BinaryExpression paramBinaryExpression, BinaryOperator paramBinaryOperator,
        Object paramObject1, Object paramObject2) {
    //      out.append("visitBinary oper ").append(paramBinaryOperator).append("\n");
    //      return null;
    BasicDBObject query = null;//from   www . j  a v a2s .  co m
    String sqlOperator = "";
    //BasicDBObject clause=null;

    DBObject clause = null;
    //      String left=null;
    //      String right=null;
    //      if (paramObject1 == null) {
    //         left=paramBinaryExpression.getLeftOperand().getUriLiteral();
    //         if (left.startsWith("'")) left=left.substring(1);
    //         if (left.endsWith("'")) left=left.substring(0, left.length()-1);
    //         left=getFullFielName(left);
    //      }
    //      if (paramObject2 == null) {
    //         right=paramBinaryExpression.getRightOperand().getUriLiteral();
    //         if (right.startsWith("'")) right=right.substring(1);
    //         if (right.endsWith("'")) right=right.substring(0, right.length()-1);
    //         right=getFullFielName(right);
    //      }

    BasicDBList lista = new BasicDBList();
    switch (paramBinaryOperator) {
    case EQ:

        if (paramBinaryExpression.getLeftOperand() instanceof PropertyExpression
                && paramBinaryExpression.getRightOperand() instanceof LiteralExpression) {
            clause = new BasicDBObject(paramObject1.toString(), paramObject2);
        } else if (paramBinaryExpression.getLeftOperand() instanceof LiteralExpression
                && paramBinaryExpression.getRightOperand() instanceof PropertyExpression) {
            clause = new BasicDBObject(paramObject2.toString(), paramObject1);
        } else if (paramObject1 instanceof BasicDBObject
                && paramBinaryExpression.getRightOperand() instanceof LiteralExpression) {
            //A sx ho una query mongo, a dx un literal ... se la query mongo deriva da una function allora 
            //prima verifica: se il literal e' un boolean ed e' un true, dovrebbe andare bene cosi'
            //                se il literal e' un boolean ed e' false, richiamo la parte che genera la query passando il false

            if (paramBinaryExpression.getRightOperand().getEdmType().toString().equals("Edm.Boolean")
                    && paramBinaryExpression.getLeftOperand() instanceof MethodExpression) {
                if (((Boolean) paramObject2).booleanValue() == false) {
                    paramObject1 = revertMethodAndBoolean(paramObject1, paramObject2,
                            paramBinaryExpression.getLeftOperand(), paramBinaryExpression.getRightOperand(),
                            true);

                    //paramObject1=visitMethod(paramMethodExpression, paramMethodOperator, paramList)
                }
                clause = (BasicDBObject) paramObject1;
            }
        } else if (paramObject2 instanceof BasicDBObject
                && paramBinaryExpression.getLeftOperand() instanceof LiteralExpression) {
            //A sx ho una query mongo, a dx un literal ... se la query mongo deriva da una function allora 
            //prima verifica: se il literal e' un boolean ed e' un true, dovrebbe andare bene cosi'
            //                se il literal e' un boolean ed e' false, richiamo la parte che genera la query passando il false

            if (paramBinaryExpression.getLeftOperand().getEdmType().toString().equals("Edm.Boolean")
                    && paramBinaryExpression.getRightOperand() instanceof MethodExpression) {
                if (((Boolean) paramObject1).booleanValue() == false) {
                    paramObject2 = revertMethodAndBoolean(paramObject2, paramObject1,
                            paramBinaryExpression.getRightOperand(), paramBinaryExpression.getLeftOperand(),
                            true);

                    //paramObject1=visitMethod(paramMethodExpression, paramMethodOperator, paramList)
                }
                clause = (BasicDBObject) paramObject2;
            }

        }

        /*
                
        sqlOperator = "=";
        if (left!=null && right!=null) 
        if (paramObject1 != null && paramObject2== null &&  paramBinaryExpression.getRightOperand().getEdmType().toString().equals("Edm.Boolean")) {
           if (right.equalsIgnoreCase("false")) {
              clause = new BasicDBObject("$not",paramObject1);
           } else {
              clause=(BasicDBObject)paramObject1;
           }
        }
        if (paramObject2 != null && paramObject1== null &&  paramBinaryExpression.getLeftOperand().getEdmType().toString().equals("Edm.Boolean")) {
           if (left.equalsIgnoreCase("false")) {
              clause = new BasicDBObject("$not",paramObject2);
           } else {
              clause=(BasicDBObject)paramObject2;
           }
        }
         */

        break;
    case NE:
        sqlOperator = "<>";
        //         if (left!=null && right!=null) clause = new BasicDBObject(left,right);
        //         if (paramObject1 != null && paramObject2== null &&  paramBinaryExpression.getRightOperand().getEdmType().toString().equals("Edm.Boolean")) {
        //            if (right.equalsIgnoreCase("false")) {
        //               clause = new BasicDBObject("$not",paramObject1);
        //            } else {
        //               clause=(BasicDBObject)paramObject1;
        //            }
        //         }
        //         if (paramObject2 != null && paramObject1== null &&  paramBinaryExpression.getLeftOperand().getEdmType().toString().equals("Edm.Boolean")) {
        //            if (left.equalsIgnoreCase("false")) {
        //               clause = new BasicDBObject("$not",paramObject2);
        //            } else {
        //               clause=(BasicDBObject)paramObject2;
        //            }
        //         }
        //
        //         BasicDBObject clauseTmp=new BasicDBObject("$not",clause);
        //         clause=clauseTmp;

        if (paramBinaryExpression.getLeftOperand() instanceof PropertyExpression
                && paramBinaryExpression.getRightOperand() instanceof LiteralExpression) {
            clause = new BasicDBObject(paramObject1.toString(), new BasicDBObject("$ne", paramObject2));
        } else if (paramBinaryExpression.getLeftOperand() instanceof LiteralExpression
                && paramBinaryExpression.getRightOperand() instanceof PropertyExpression) {
            clause = new BasicDBObject(paramObject2.toString(), new BasicDBObject("$ne", paramObject1));
        } else if (paramObject1 instanceof BasicDBObject
                && paramBinaryExpression.getRightOperand() instanceof LiteralExpression) {
            //A sx ho una query mongo, a dx un literal ... se la query mongo deriva da una function allora 
            //prima verifica: se il literal e' un boolean ed e' un true, dovrebbe andare bene cosi'
            //                se il literal e' un boolean ed e' false, richiamo la parte che genera la query passando il false

            if (paramBinaryExpression.getRightOperand().getEdmType().toString().equals("Edm.Boolean")
                    && paramBinaryExpression.getLeftOperand() instanceof MethodExpression) {
                if (((Boolean) paramObject2).booleanValue() == true) {
                    paramObject1 = revertMethodAndBoolean(paramObject1, paramObject2,
                            paramBinaryExpression.getLeftOperand(), paramBinaryExpression.getRightOperand(),
                            true);

                    //paramObject1=visitMethod(paramMethodExpression, paramMethodOperator, paramList)
                }
                clause = (BasicDBObject) paramObject1;
            }
        } else if (paramObject2 instanceof BasicDBObject
                && paramBinaryExpression.getLeftOperand() instanceof LiteralExpression) {
            //A sx ho una query mongo, a dx un literal ... se la query mongo deriva da una function allora 
            //prima verifica: se il literal e' un boolean ed e' un true, dovrebbe andare bene cosi'
            //                se il literal e' un boolean ed e' false, richiamo la parte che genera la query passando il false

            if (paramBinaryExpression.getLeftOperand().getEdmType().toString().equals("Edm.Boolean")
                    && paramBinaryExpression.getRightOperand() instanceof MethodExpression) {
                if (((Boolean) paramObject1).booleanValue() == true) {
                    paramObject2 = revertMethodAndBoolean(paramObject2, paramObject1,
                            paramBinaryExpression.getRightOperand(), paramBinaryExpression.getLeftOperand(),
                            true);

                    //paramObject1=visitMethod(paramMethodExpression, paramMethodOperator, paramList)
                }
                clause = (BasicDBObject) paramObject2;
            }
        }

        break;
    case OR:
        sqlOperator = "OR";

        lista.add(paramObject1);
        lista.add(paramObject2);
        clause = new BasicDBObject("$or", lista);
        //return clause;

        break;
    case AND:
        sqlOperator = "AND";

        lista.add(paramObject1);
        lista.add(paramObject2);
        clause = new BasicDBObject("$and", lista);
        //return clause;

        break;
    case GE:
        sqlOperator = ">=";

        if (paramBinaryExpression.getLeftOperand() instanceof PropertyExpression
                && paramBinaryExpression.getRightOperand() instanceof LiteralExpression) {
            clause = new BasicDBObject(paramObject1.toString(), new BasicDBObject("$gte", paramObject2));
        } else if (paramBinaryExpression.getLeftOperand() instanceof LiteralExpression
                && paramBinaryExpression.getRightOperand() instanceof PropertyExpression) {
            clause = new BasicDBObject(paramObject2.toString(), new BasicDBObject("$lte", paramObject1));
        }

        break;
    case GT:
        sqlOperator = ">";
        if (paramBinaryExpression.getLeftOperand() instanceof PropertyExpression
                && paramBinaryExpression.getRightOperand() instanceof LiteralExpression) {
            clause = new BasicDBObject(paramObject1.toString(), new BasicDBObject("$gt", paramObject2));
        } else if (paramBinaryExpression.getLeftOperand() instanceof LiteralExpression
                && paramBinaryExpression.getRightOperand() instanceof PropertyExpression) {
            clause = new BasicDBObject(paramObject2.toString(), new BasicDBObject("$le", paramObject1));
        }
        break;
    case LE:
        sqlOperator = "<=";
        if (paramBinaryExpression.getLeftOperand() instanceof PropertyExpression
                && paramBinaryExpression.getRightOperand() instanceof LiteralExpression) {
            clause = new BasicDBObject(paramObject1.toString(), new BasicDBObject("$lte", paramObject2));
        } else if (paramBinaryExpression.getLeftOperand() instanceof LiteralExpression
                && paramBinaryExpression.getRightOperand() instanceof PropertyExpression) {
            clause = new BasicDBObject(paramObject2.toString(), new BasicDBObject("$gte", paramObject1));
        }
        break;
    case LT:
        sqlOperator = "<";
        if (paramBinaryExpression.getLeftOperand() instanceof PropertyExpression
                && paramBinaryExpression.getRightOperand() instanceof LiteralExpression) {
            clause = new BasicDBObject(paramObject1.toString(), new BasicDBObject("$lt", paramObject2));
        } else if (paramBinaryExpression.getLeftOperand() instanceof LiteralExpression
                && paramBinaryExpression.getRightOperand() instanceof PropertyExpression) {
            clause = new BasicDBObject(paramObject2.toString(), new BasicDBObject("$gt", paramObject1));
        }
        break;
    default:
        //Other operators are not supported for SQL Statements
        throw new UnsupportedOperationException("Unsupported operator: " + paramBinaryOperator.toUriLiteral());
    }
    //return the binary statement

    //       if (paramObject1 != null && paramObject2!=null) return paramObject1 + " " + sqlOperator + " " + paramObject2; 
    //       else return paramBinaryExpression.getLeftOperand().getUriLiteral() + " " + sqlOperator + " " + paramBinaryExpression.getRightOperand().getUriLiteral();

    return clause;

}

From source file:it.csi.smartdata.odata.datadiscovery.SDPExpressionVisitor.java

@Override
public Object visitOrderByExpression(OrderByExpression paramOrderByExpression, String paramString,
        List<Object> paramList) {
    out.append("visitOrderByExpression\n");

    BasicDBList ret = new BasicDBList();
    for (int i = 0; paramList != null && i < paramList.size(); i++) {
        if (paramList.get(i) instanceof BasicDBObject) {
            ret.add(paramList.get(i));/*ww w. ja  v  a2s  . c o  m*/
        }
    }
    if (ret.size() > 0)
        return ret;
    return null;
}

From source file:japura.Tribes.TeleportData.java

License:BSD License

public TeleportData(Block spot, String name, Tribe owner) {
    this.name = name;
    this.owner = owner;
    this.spot = spot;

    BasicDBObject query = new BasicDBObject();
    query.put("tribe", owner.getName());
    query.put("name", name);
    myTeleport = (BasicDBObject) Tribes.getDiamondTable().findOne(query);

    if (myTeleport != null) {
        allowed = (BasicDBList) myTeleport.get("allowed");

        long x = myTeleport.getLong("X");
        long y = myTeleport.getLong("Y");
        long z = myTeleport.getLong("Z");
        World myWorld = Bukkit.getWorld((String) myTeleport.get("world"));
        Location loc1 = new Location(myWorld, x, y, z);
        if (!spot.equals(loc1.getBlock())) {
            Tribes.log("invalid diamond in DB");
        }/*  www .  j  a v a 2s .c om*/
        return;
    }
    //else, create this teleport
    myTeleport = new BasicDBObject();
    myTeleport.put("tribe", owner.getName());
    myTeleport.put("name", name);
    myTeleport.put("X", spot.getLocation().getBlockX());
    myTeleport.put("Y", spot.getLocation().getBlockY());
    myTeleport.put("Z", spot.getLocation().getBlockZ());
    myTeleport.put("world", spot.getLocation().getWorld().getName());
    allowed = new BasicDBList();
    allowed.add(owner.getName());
    myTeleport.put("allowed", allowed);

    Tribes.getDiamondTable().insert(myTeleport);
}

From source file:japura.Tribes.Tribe.java

License:BSD License

public Tribe(String name) {
    if (name == null) {
        Tribes.log("attempted to get tribe null");
        return;/*ww  w.  j ava  2s.  co  m*/
    }
    this.name = name.toLowerCase();
    BasicDBObject query = new BasicDBObject();
    query.put("name", this.name);

    myTribe = Tribes.getTribeTable().findOne(query);

    if (myTribe == null) {
        this.name = "invalid tribe";
        leader = "invalid tribe leader";
        valid = false;
    } else {
        Tribes.invalidateTribeNames();
        valid = true;
        users = (BasicDBList) myTribe.get("members");
        if (users == null) {
            users = new BasicDBList();
        }
        invites = (BasicDBList) myTribe.get("invites");
        if (invites == null) {
            invites = new BasicDBList();
        }

        leader = (String) myTribe.get("leader");
        if (leader == null) {
            leader = "invalid leader";
        }
    }
}

From source file:kiaanfx.Kiaanfx.java

private static void getBuy() {
    try {// w w w. j  av  a2s . c  om
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("kiaan");
        DBCollection coll = db.getCollection("buy");
        //aggregate
        DBObject unwind = new BasicDBObject("$unwind", "$items");
        //$group            
        DBObject group_id = new BasicDBObject("_id", "$_id");
        group_id.put("num", "$num");
        group_id.put("person_id", "$person_id");
        group_id.put("discount", "$discount");
        group_id.put("increase", "$increase");
        //$group -> $multiply
        BasicDBList args = new BasicDBList();
        args.add("$items.value");
        args.add("$items.price");
        DBObject multiply = new BasicDBObject("$multiply", args);
        //$group -> $sum
        //            DBObject group_sum = new BasicDBObject("$sum", multiply);
        DBObject group_field = new BasicDBObject();
        group_field.put("_id", group_id);
        group_field.put("total", new BasicDBObject("$sum", multiply));
        DBObject group = new BasicDBObject("$group", group_field);
        //$project
        DBObject project_field = new BasicDBObject("_id", "$_id._id");
        project_field.put("person_id", "$_id.person_id");
        project_field.put("num", "$_id.num");
        BasicDBList arr = new BasicDBList();
        arr.add("$total");
        arr.add("$_id.discount");
        arr.add("$_id.increase");
        DBObject field_add = new BasicDBObject("$add", arr);
        project_field.put("sum", field_add);
        DBObject project = new BasicDBObject("$project", project_field);
        DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1));
        List<DBObject> pipeline = Arrays.asList(unwind, group, project, sort);

        //            AggregationOutput output = coll.aggregate(pipeline);
        //            for (DBObject result : output.results()) {
        //                System.out.println(result);
        //            }

        AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
                .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

        BasicDBObject dbo = new BasicDBObject();
        BasicDBList dbl = new BasicDBList();
        Cursor cursor = coll.aggregate(pipeline, aggregationOptions);
        //            
        DBCollection person_col = db.getCollection("persons");

        //            BasicDBObject query = new BasicDBObject("items.personId",1);             
        BasicDBObject fields = new BasicDBObject("items.$", 1).append("_id", false);

        //            BasicDBList l_per = (BasicDBList) person_col.findOne(query, fields).get("items");
        //            BasicDBObject[] lightArr = l_per.toArray(new BasicDBObject[0]);            
        //            System.out.println(lightArr[0].get("_id"));
        //            System.out.println(lightArr[0].get("first_name"));  

        //            BasicDBList result = new BasicDBList();
        while (cursor.hasNext()) {
            dbo = (BasicDBObject) cursor.next();
            //                System.out.println(dbo.toString());  
            DBObject query = new BasicDBObject("items._id", (ObjectId) dbo.get("person_id"));
            BasicDBList lst_person = (BasicDBList) person_col.findOne(query, fields).get("items");
            BasicDBObject[] lightArr = lst_person.toArray(new BasicDBObject[0]);
            //                System.out.println(lightArr[0].get("first_name"));

            Date date = ((ObjectId) lightArr[0].get("_id")).getDate();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            persianCalendar persianCalendar = new persianCalendar(calendar);

            dbo.put("date", persianCalendar.getNumericDateFormatWithTime());
            dbo.put("personId", lightArr[0].get("personId").toString());
            dbo.put("first_name", lightArr[0].get("first_name").toString());
            dbo.put("last_name", lightArr[0].get("last_name").toString());

            data.add(new Person(dbo.get("num").toString(), dbo.get("date").toString(),
                    dbo.get("personId").toString(), dbo.get("first_name").toString(),
                    dbo.get("last_name").toString()));
            //                buy_data.add(new buys(dbo.get("num").toString(),
            //                        dbo.get("date").toString(), 
            //                        dbo.get("personId").toString(),
            //                        dbo.get("first_name").toString(),
            //                        dbo.get("last_name").toString(),
            //                        dbo.get("sum").toString()
            //                ));                                
            dbo.remove("person_id");
            //                result.add(dbo);                
            //                System.out.println(dbo.get("first_name"));                  
        }
        System.out.println(dbo.toString());

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:laboratorio_2_sd.IndiceInvertido.java

public BasicDBObject toDBObject() {
    BasicDBObject dBObject = new BasicDBObject();
    dBObject.append("idPalabra", this.idPalabra);
    BasicDBList frecuenciasDB = new BasicDBList();
    for (int i = 0; i < frecuencias.size(); i++) {
        frecuenciasDB.add(frecuencias.get(i).toDBObject());
    }// w w w.  j  av  a 2 s .c o m
    dBObject.append("frecuencias", frecuenciasDB);

    return dBObject;
}

From source file:MDBInt.DBMongo.java

License:Apache License

/**
 * /*from  w  ww .j  a v  a 2s .c  om*/
 * @param dbName
 * @param faSite
 * @param key
 * @param list
 * @return 
 * @author agalletta
 */
public Boolean checkNetTables(String dbName, String faSite, String key, LinkedHashSet<String> list) {

    DB database = this.getDB(dbName);
    DBCollection collection = database.getCollection("netTables");
    //DBCollection collection = database.getCollection("testIn");

    BasicDBObject tableQuery, andQuery;
    BasicDBList and;
    double version;
    BasicDBObject resQuery = new BasicDBObject("referenceSite", faSite);
    BasicDBObject sortQuery = new BasicDBObject("version", -1);
    DBCursor b = collection.find(resQuery).sort(sortQuery).limit(1);
    Iterator<String> it;
    if (!b.hasNext()) {
        //System.out.println("tabella non trovata");
        return null;
    } else {
        version = (double) b.next().get("version");
        //System.out.println(version);

        //andQuery=new BasicDBObject("referenceSite",faSite);
        and = new BasicDBList();
        and.add(resQuery);
        and.add(new BasicDBObject("version", version));
        it = list.iterator();
        while (it.hasNext()) {
            // resQuery.append("table." + key, it.next());
            and.add(new BasicDBObject("table." + key, it.next()));
        }
        andQuery = new BasicDBObject("$and", and);
        //System.out.println("query:" + andQuery);
        b = collection.find(andQuery);
        if (!b.hasNext()) {
            //  System.out.println("tabella incompleta");
            return false;
        } else {
            return true;

        }

    }
}

From source file:MDBInt.DBMongo.java

License:Apache License

/**
 * //from  ww  w . ja v  a  2  s.  c  o m
 * @param dbName
 * @param faSite
 * @param key
 * @param list
 * @return 
 * @author agalletta
 */
public Boolean checkSiteTables(String dbName, String faSite, String key, LinkedHashSet<String> list) {

    DB database = this.getDB(dbName);
    DBCollection collection = database.getCollection("siteTables");
    //DBCollection collection = database.getCollection("testIn");

    BasicDBObject tableQuery, andQuery;
    BasicDBList and;
    double version;
    BasicDBObject resQuery = new BasicDBObject("referenceSite", faSite);
    BasicDBObject sortQuery = new BasicDBObject("version", -1);
    DBCursor b = collection.find(resQuery).sort(sortQuery).limit(1);
    Iterator<String> it;
    if (!b.hasNext()) {
        // System.out.println("tabella non trovata");
        return null;
    } else {
        version = (double) b.next().get("version");
        // System.out.println(version);

        //andQuery=new BasicDBObject("referenceSite",faSite);
        and = new BasicDBList();
        and.add(resQuery);
        and.add(new BasicDBObject("version", version));
        it = list.iterator();
        while (it.hasNext()) {
            // resQuery.append("table." + key, it.next());
            and.add(new BasicDBObject("table" + key, it.next()));
        }
        andQuery = new BasicDBObject("$and", and);
        // System.out.println("query:" + andQuery);
        b = collection.find(andQuery);
        if (!b.hasNext()) {
            //   System.out.println("tabella incompleta");
            return false;
        } else {
            return true;

        }

    }
}