List of usage examples for com.mongodb BasicDBList BasicDBList
BasicDBList
From source file:com.owly.srv.RemoteServerMongoDAOImpl.java
License:Apache License
public Integer numberRemoteServer(ArrayList<ShortRemoteServerId> listShortRemoteServerId) { logger.debug("Array received" + listShortRemoteServerId.toString()); BasicDBObject query = new BasicDBObject(); BasicDBList or = new BasicDBList(); Iterator<ShortRemoteServerId> iterator = listShortRemoteServerId.iterator(); while (iterator.hasNext()) { ShortRemoteServerId shortRmt = iterator.next(); BasicDBObject clause = new BasicDBObject("NodeIPAddress", shortRmt.getNodeIPAddress()).append("Name", shortRmt.getName());//from w w w .j av a 2 s . c o m or.add(clause); } query = new BasicDBObject("$or", or); logger.info("MONGODB : Get number of all objects in the database for this clause" + query.toString()); Integer num = remoteSrvCfgCollection.find(query).count(); logger.info("MONGODB : Number of all objects in the database obtained"); return num; }
From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java
License:Apache License
public long countConflicts(Filter filter, List<String> properties) { LOG.info("Calculating conflicts count"); DBCollection collection = this.getCollection(Element.class); List<DBObject> list = new ArrayList<DBObject>(); list.add(new BasicDBObject("$match", this.getCachedFilter(filter))); list.add(new BasicDBObject("$unwind", "$metadata")); list.add(new BasicDBObject("$project", new BasicDBObject("status", "$metadata.status").append("uid", 1) .append("property", "$metadata.property"))); list.add(new BasicDBObject("$match", new BasicDBObject("property", new BasicDBObject("$in", properties)))); list.add(new BasicDBObject("$group", new BasicDBObject("_id", "$uid").append("statuses", new BasicDBObject("$addToSet", "$status")))); BasicDBList in = new BasicDBList(); in.add("CONFLICT"); list.add(new BasicDBObject("$match", new BasicDBObject("statuses", new BasicDBObject("$in", in)))); list.add(new BasicDBObject("$group", new BasicDBObject("_id", null).append("count", new BasicDBObject("$sum", 1)))); Iterable<DBObject> resultIterable = collection.aggregate(list).results(); BasicDBObject result = (BasicDBObject) resultIterable.iterator().next(); return result.getLong("count"); }
From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java
License:Apache License
/** * Checks if the {@link DBCache} has a filter that equals the given filter. If * yes, then the object that is stored under the last filter query key within * the cache is returned. If the last filter is null, or does not equal, then * the cache is update and the correct filter is returned. * * @param f the filter to check./*from ww w . ja v a2s. c o m*/ * @return the cached filter or the updated version. * @see MongoFilterSerializer; */ public DBObject getCachedFilter(Filter f) { Filter filter = (Filter) this.dbCache.getObject(LAST_FILTER); DBObject result = new BasicDBList(); result = this.filterSerializer.serializeNew(f); return result; }
From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java
License:Apache License
public List<BasicDBObject> aggregate(String property, Filter filter, Boolean getStats) { LOG.debug("Starting aggregation for the following property: {}", property); long start = System.currentTimeMillis(); Property prop = getCache().getProperty(property); String propType = prop.getType(); List<BasicDBObject> result = new ArrayList<BasicDBObject>(); DBCollection collection = this.getCollection(Element.class); BasicDBList basicAggregationStages = new BasicDBList(); BasicDBList basicDBList = new BasicDBList(); if (propType.equals(PropertyType.STRING.toString())) { basicAggregationStages = getBasicAggregationStages(property, filter, "$sourcedValue.value"); } else if (propType.equals(PropertyType.INTEGER.toString()) || propType.equals(PropertyType.FLOAT.toString())) { //TODO: choose a better strategy to address this. Think of bins for numerical values. basicAggregationStages = getBasicAggregationStages(property, filter, "$sourcedValue.value"); } else if (propType.equals(PropertyType.DATE.toString())) { /* BasicDBList cond = new BasicDBList(); BasicDBList eq = new BasicDBList(); eq.add("$sourcedValue.value");// w w w . j a va2s. co m eq.add(0); cond.add(new BasicDBObject("$ifNull", eq)); cond.add(new BasicDBObject("$year", "$sourcedValue.value")); cond.add(-1); */ BasicDBObject conditionalValue = new BasicDBObject("$year", "$sourcedValue.value"); basicAggregationStages = getBasicAggregationStages(property, filter, conditionalValue); } else if (propType.equals(PropertyType.BOOL.toString())) { basicAggregationStages = getBasicAggregationStages(property, filter, "$sourcedValue.value"); } if (getStats) basicAggregationStages.add(new BasicDBObject("$group", new BasicDBObject("_id", "$property") .append("stdDev", new BasicDBObject("$stdDevPop", "$value")) .append("min", new BasicDBObject("$min", "$value")) .append("max", new BasicDBObject("$max", "$value")) .append("avg", new BasicDBObject("$avg", "$value")) .append("sum", new BasicDBObject("$sum", "$value")) .append("count", new BasicDBObject("$sum", 1)))); else { if (propType.equals(PropertyType.INTEGER.toString())) basicAggregationStages.add(new BasicDBObject("$bucketAuto", new BasicDBObject("groupBy", "$value").append("buckets", 10))); // basicAggregationStages.add(new BasicDBObject("$group", new BasicDBObject("_id", "$value").append("count", new BasicDBObject("$sum", 1)))); } //AggregationOutput aggregate = collection.aggregate(basicAggregationStages); String s = basicAggregationStages.toString(); List<DBObject> pipeline = new ArrayList<DBObject>(); for (Object basicAggregationStage : basicAggregationStages) { pipeline.add((DBObject) basicAggregationStage); } AggregationOptions build = AggregationOptions.builder().allowDiskUse(true).build(); Cursor aggregate = collection.aggregate(pipeline, build); // while(aggregate.hasNext()){ // result.add((BasicDBObject) aggregate.next()); // } //Iterable<DBObject> resultIterable = collection.aggregate(pipeline,build).results(); //for (DBObject object : resultIterable) { // result.add((BasicDBObject) object); //} long end = System.currentTimeMillis(); LOG.debug("The aggregation job took {} seconds", (end - start) / 1000); return result; }
From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java
License:Apache License
private BasicDBList getBasicAggregationStages(String property, Filter filter, Object conditionalValue) { BasicDBList list = new BasicDBList(); //list.add(new BasicDBObject("$match", this.getCachedFilter(filter))); list.add(new BasicDBObject("$unwind", "$metadata")); list.add(new BasicDBObject("$match", new BasicDBObject("metadata.property", property))); BasicDBList arrayElemAt = new BasicDBList(); arrayElemAt.add("$metadata.sourcedValues"); arrayElemAt.add(0);/*w w w. ja v a 2 s . co m*/ list.add(new BasicDBObject("$project", new BasicDBObject("status", "$metadata.status").append("property", "$metadata.property") .append("sourcedValue", new BasicDBObject("$arrayElemAt", arrayElemAt)))); //list.add(new BasicDBObject("$project", new BasicDBObject("value", conditionalValue).append("property",1).append("source", "$sourcedValue.source").append("status",1)));// new BasicDBObject("$cond", cond)).append("property", 1))); BasicDBList cond = new BasicDBList(); BasicDBList eq = new BasicDBList(); eq.add("$status"); eq.add("CONFLICT"); cond.add(new BasicDBObject("$eq", eq)); cond.add("CONFLICT"); cond.add("$value"); //list.add(new BasicDBObject("$project", new BasicDBObject("value", new BasicDBObject("$cond", cond)).append("property", 1))); return list; }
From source file:com.ptoceti.mongo.geoname.GeoNameLoc.java
License:Open Source License
public void setAlternateNames(List<GeoNameAlternateName> alternateNames) { BasicDBList list = new BasicDBList(); for (GeoNameAlternateName item : alternateNames) { list.add(item);/*from w w w. j av a2 s . c om*/ } put(CC2, list); }
From source file:com.ptoceti.mongo.geoname.GeoNameLoc.java
License:Open Source License
public void setCc2(List<String> cc2) { BasicDBList list = new BasicDBList(); for (String item : cc2) { list.add(item);// w w w . j a v a2 s . co m } put(CC2, list); }
From source file:com.querydsl.mongodb.MongodbSerializer.java
License:Apache License
@SuppressWarnings("unchecked") @Override//from ww w .java2 s . c om public Object visit(Operation<?> expr, Void context) { Operator op = expr.getOperator(); if (op == Ops.EQ) { if (expr.getArg(0) instanceof Operation) { Operation<?> lhs = (Operation<?>) expr.getArg(0); if (lhs.getOperator() == Ops.COL_SIZE || lhs.getOperator() == Ops.ARRAY_SIZE) { return asDBObject(asDBKey(lhs, 0), asDBObject("$size", asDBValue(expr, 1))); } else { throw new UnsupportedOperationException("Illegal operation " + expr); } } else if (expr.getArg(0) instanceof Path) { Path<?> path = (Path<?>) expr.getArg(0); Constant<?> constant = (Constant<?>) expr.getArg(1); return asDBObject(asDBKey(expr, 0), convert(path, constant)); } } else if (op == Ops.STRING_IS_EMPTY) { return asDBObject(asDBKey(expr, 0), ""); } else if (op == Ops.AND) { BSONObject lhs = (BSONObject) handle(expr.getArg(0)); BSONObject rhs = (BSONObject) handle(expr.getArg(1)); if (Sets.intersection(lhs.keySet(), rhs.keySet()).isEmpty()) { lhs.putAll(rhs); return lhs; } else { BasicDBList list = new BasicDBList(); list.add(handle(expr.getArg(0))); list.add(handle(expr.getArg(1))); return asDBObject("$and", list); } } else if (op == Ops.NOT) { //Handle the not's child Operation<?> subOperation = (Operation<?>) expr.getArg(0); Operator subOp = subOperation.getOperator(); if (subOp == Ops.IN) { return visit(ExpressionUtils.operation(Boolean.class, Ops.NOT_IN, subOperation.getArg(0), subOperation.getArg(1)), context); } else { BasicDBObject arg = (BasicDBObject) handle(expr.getArg(0)); return negate(arg); } } else if (op == Ops.OR) { BasicDBList list = new BasicDBList(); list.add(handle(expr.getArg(0))); list.add(handle(expr.getArg(1))); return asDBObject("$or", list); } else if (op == Ops.NE) { Path<?> path = (Path<?>) expr.getArg(0); Constant<?> constant = (Constant<?>) expr.getArg(1); return asDBObject(asDBKey(expr, 0), asDBObject("$ne", convert(path, constant))); } else if (op == Ops.STARTS_WITH) { return asDBObject(asDBKey(expr, 0), Pattern.compile("^" + regexValue(expr, 1))); } else if (op == Ops.STARTS_WITH_IC) { return asDBObject(asDBKey(expr, 0), Pattern.compile("^" + regexValue(expr, 1), Pattern.CASE_INSENSITIVE)); } else if (op == Ops.ENDS_WITH) { return asDBObject(asDBKey(expr, 0), Pattern.compile(regexValue(expr, 1) + "$")); } else if (op == Ops.ENDS_WITH_IC) { return asDBObject(asDBKey(expr, 0), Pattern.compile(regexValue(expr, 1) + "$", Pattern.CASE_INSENSITIVE)); } else if (op == Ops.EQ_IGNORE_CASE) { return asDBObject(asDBKey(expr, 0), Pattern.compile("^" + regexValue(expr, 1) + "$", Pattern.CASE_INSENSITIVE)); } else if (op == Ops.STRING_CONTAINS) { return asDBObject(asDBKey(expr, 0), Pattern.compile(".*" + regexValue(expr, 1) + ".*")); } else if (op == Ops.STRING_CONTAINS_IC) { return asDBObject(asDBKey(expr, 0), Pattern.compile(".*" + regexValue(expr, 1) + ".*", Pattern.CASE_INSENSITIVE)); } else if (op == Ops.MATCHES) { return asDBObject(asDBKey(expr, 0), Pattern.compile(asDBValue(expr, 1).toString())); } else if (op == Ops.MATCHES_IC) { return asDBObject(asDBKey(expr, 0), Pattern.compile(asDBValue(expr, 1).toString(), Pattern.CASE_INSENSITIVE)); } else if (op == Ops.LIKE) { String regex = ExpressionUtils.likeToRegex((Expression) expr.getArg(1)).toString(); return asDBObject(asDBKey(expr, 0), Pattern.compile(regex)); } else if (op == Ops.BETWEEN) { BasicDBObject value = new BasicDBObject("$gte", asDBValue(expr, 1)); value.append("$lte", asDBValue(expr, 2)); return asDBObject(asDBKey(expr, 0), value); } else if (op == Ops.IN) { int constIndex = 0; int exprIndex = 1; if (expr.getArg(1) instanceof Constant<?>) { constIndex = 1; exprIndex = 0; } if (Collection.class.isAssignableFrom(expr.getArg(constIndex).getType())) { @SuppressWarnings("unchecked") //guarded by previous check Collection<?> values = ((Constant<? extends Collection<?>>) expr.getArg(constIndex)).getConstant(); return asDBObject(asDBKey(expr, exprIndex), asDBObject("$in", values.toArray())); } else { Path<?> path = (Path<?>) expr.getArg(exprIndex); Constant<?> constant = (Constant<?>) expr.getArg(constIndex); return asDBObject(asDBKey(expr, exprIndex), convert(path, constant)); } } else if (op == Ops.NOT_IN) { int constIndex = 0; int exprIndex = 1; if (expr.getArg(1) instanceof Constant<?>) { constIndex = 1; exprIndex = 0; } if (Collection.class.isAssignableFrom(expr.getArg(constIndex).getType())) { @SuppressWarnings("unchecked") //guarded by previous check Collection<?> values = ((Constant<? extends Collection<?>>) expr.getArg(constIndex)).getConstant(); return asDBObject(asDBKey(expr, exprIndex), asDBObject("$nin", values.toArray())); } else { Path<?> path = (Path<?>) expr.getArg(exprIndex); Constant<?> constant = (Constant<?>) expr.getArg(constIndex); return asDBObject(asDBKey(expr, exprIndex), asDBObject("$ne", convert(path, constant))); } } else if (op == Ops.COL_IS_EMPTY) { BasicDBList list = new BasicDBList(); list.add(asDBObject(asDBKey(expr, 0), new BasicDBList())); list.add(asDBObject(asDBKey(expr, 0), asDBObject("$exists", false))); return asDBObject("$or", list); } else if (op == Ops.LT) { return asDBObject(asDBKey(expr, 0), asDBObject("$lt", asDBValue(expr, 1))); } else if (op == Ops.GT) { return asDBObject(asDBKey(expr, 0), asDBObject("$gt", asDBValue(expr, 1))); } else if (op == Ops.LOE) { return asDBObject(asDBKey(expr, 0), asDBObject("$lte", asDBValue(expr, 1))); } else if (op == Ops.GOE) { return asDBObject(asDBKey(expr, 0), asDBObject("$gte", asDBValue(expr, 1))); } else if (op == Ops.IS_NULL) { return asDBObject(asDBKey(expr, 0), asDBObject("$exists", false)); } else if (op == Ops.IS_NOT_NULL) { return asDBObject(asDBKey(expr, 0), asDBObject("$exists", true)); } else if (op == Ops.CONTAINS_KEY) { Path<?> path = (Path<?>) expr.getArg(0); Expression<?> key = expr.getArg(1); return asDBObject(visit(path, context) + "." + key.toString(), asDBObject("$exists", true)); } else if (op == MongodbOps.NEAR) { return asDBObject(asDBKey(expr, 0), asDBObject("$near", asDBValue(expr, 1))); } else if (op == MongodbOps.NEAR_SPHERE) { return asDBObject(asDBKey(expr, 0), asDBObject("$nearSphere", asDBValue(expr, 1))); } else if (op == MongodbOps.ELEM_MATCH) { return asDBObject(asDBKey(expr, 0), asDBObject("$elemMatch", asDBValue(expr, 1))); } throw new UnsupportedOperationException("Illegal operation " + expr); }
From source file:com.querydsl.mongodb.MongodbSerializer.java
License:Apache License
private Object negate(BasicDBObject arg) { BasicDBList list = new BasicDBList(); for (Map.Entry<String, Object> entry : arg.entrySet()) { if (entry.getKey().equals("$or")) { list.add(asDBObject("$nor", entry.getValue())); } else if (entry.getKey().equals("$and")) { BasicDBList list2 = new BasicDBList(); for (Object o : ((BasicDBList) entry.getValue())) { list2.add(negate((BasicDBObject) o)); }/* w ww . j a v a2 s . c o m*/ list.add(asDBObject("$or", list2)); } else if (entry.getValue() instanceof Pattern) { list.add(asDBObject(entry.getKey(), asDBObject("$not", entry.getValue()))); } else if (entry.getValue() instanceof BasicDBObject) { list.add(negate(entry.getKey(), (BasicDBObject) entry.getValue())); } else { list.add(asDBObject(entry.getKey(), asDBObject("$ne", entry.getValue()))); } } return list.size() == 1 ? list.get(0) : asDBObject("$or", list); }
From source file:com.querydsl.mongodb.MongodbSerializer.java
License:Apache License
private Object negate(String key, BasicDBObject value) { if (value.size() == 1) { return asDBObject(key, asDBObject("$not", value)); } else {//from w w w . j av a 2 s. com BasicDBList list2 = new BasicDBList(); for (Map.Entry<String, Object> entry2 : value.entrySet()) { list2.add(asDBObject(key, asDBObject("$not", asDBObject(entry2.getKey(), entry2.getValue())))); } return asDBObject("$or", list2); } }