List of usage examples for com.mongodb QueryOperators GT
String GT
To view the source code for com.mongodb QueryOperators GT.
Click Source Link
From source file:fr.cnes.sitools.dataset.database.mongodb.OperatorMongoDB.java
License:Open Source License
/** * Get the operator value for a MongoDB query * /*from w ww . j av a2 s .co m*/ * @param operator * the {@link Operator} * @return the string representation of this operator for MongoDB */ public static String getOperatorValue(Operator operator) { String value; switch (operator) { case EQ: value = null; break; case GT: value = QueryOperators.GT; break; case GTE: value = QueryOperators.GTE; break; case IN: value = QueryOperators.IN; break; case LIKE: value = "LIKE"; break; case LT: value = QueryOperators.LT; break; case LTE: value = QueryOperators.LTE; break; case NOTIN: value = QueryOperators.NIN; break; case GEO_OVERLAP: value = "$within"; break; case NE: value = QueryOperators.NE; break; default: value = null; } return value; }
From source file:info.bunji.mongodb.synces.CollectionExtractor.java
License:Apache License
/** **********************************/*from www. ja v a 2 s .c om*/ * * @param filter * @param id * @return ********************************** */ private BasicDBObject getFilterForInitialImport(BasicDBObject filter, Object id) { if (id == null) { return filter; } BasicDBObject idFilter = new BasicDBObject("_id", new BasicBSONObject(QueryOperators.GT, id)); if (filter == null || filter.equals(new BasicDBObject())) { return idFilter; } return new BasicDBObject(QueryOperators.AND, ImmutableList.of(filter, idFilter)); }
From source file:jmockmongo.DefaultQueryHandler.java
License:Open Source License
public BSONObject[] handleQuery(String database, String collection, BSONObject command) { if ("system.indexes".equals(collection)) return new BSONObject[0]; // https://jira.mongodb.org/browse/SERVER-6078 BSONObject sort = null;/* w w w . j ava2s. co m*/ if (command.containsField("query")) { Object q = command.get("query"); if (q instanceof BSONObject) { BSONObject qq = (BSONObject) q; Unsupported.supportedFields(command, "query", "orderby"); sort = BSONUtils.getObject(command, "orderby"); command = qq; if (sort != null && sort.keySet().isEmpty()) sort = null; } } MockDB db = mongo.getDB(database); if (db != null) { if (command.keySet().isEmpty()) return findAll(db, database, collection, sort); Object id = null; Map<String, List<QueryPredicate>> filters = new HashMap<String, List<QueryPredicate>>(); for (String field : command.keySet()) { if ("_id".equals(field)) { id = command.get(field); if (id instanceof BSONObject) { BSONObject options = (BSONObject) id; for (String s : options.keySet()) { if (s.equals("$ref") || s.equals("$id")) continue; if (QueryOperators.GT.equals(s)) { multiPut(filters, field, new GreaterThan(options.get(s))); id = null; } else if (QueryOperators.LT.equals(s)) { multiPut(filters, field, new LowerThan(options.get(s))); id = null; } else if (QueryOperators.GTE.equals(s)) { multiPut(filters, field, new GreaterThanOrEqual(options.get(s))); id = null; } else if (QueryOperators.LTE.equals(s)) { multiPut(filters, field, new LowerThanOrEqual(options.get(s))); id = null; } else if (s.startsWith("$")) throw new UnsupportedOperationException( s + " queries are not implemented:" + command); } } } else { if (field.startsWith("$")) throw new UnsupportedOperationException(field + " queries are not implemented:" + command); if (field.contains(".")) throw new UnsupportedOperationException( "nested field queries are not implemented: " + command); Object value = command.get(field); if (value instanceof String || value instanceof ObjectId || value instanceof Long || value instanceof Integer) { multiPut(filters, field, new Equality(new Object[] { value })); } else if (value instanceof BSONObject) { BSONObject options = (BSONObject) value; for (String f : options.keySet()) { if ("$in".equals(f)) { multiPut(filters, field, new Equality(BSONUtils.values(options, f))); } else if (QueryOperators.GT.equals(f)) { multiPut(filters, field, new GreaterThan(options.get(f))); } else if (QueryOperators.LT.equals(f)) { multiPut(filters, field, new LowerThan(options.get(f))); } else if (QueryOperators.GTE.equals(f)) { multiPut(filters, field, new GreaterThanOrEqual(options.get(f))); } else if (QueryOperators.LTE.equals(f)) { multiPut(filters, field, new LowerThanOrEqual(options.get(f))); } else { throw new UnsupportedOperationException( "unsupported query " + f + " for " + field + ": " + options); } } } else throw new UnsupportedOperationException("unsupported query for " + field + ": " + command); } } BSONObject[] all = null; if (id == null) { all = findAll(db, database, collection, sort); } else { MockDBCollection c = db.getCollection(collection); if (c != null) { BSONObject result = c.findOne(id); if (result != null) all = new BSONObject[] { result }; } } if (all != null && all.length > 0) { if (filters.isEmpty()) return all; List<BSONObject> result = new ArrayList<BSONObject>(all.length); candidates: for (BSONObject x : all) { for (Map.Entry<String, List<QueryPredicate>> e : filters.entrySet()) { Object xx = x.get(e.getKey()); for (QueryPredicate q : e.getValue()) if (!q.test(xx)) continue candidates; } result.add(x); } return result.toArray(new BSONObject[0]); } } return new BSONObject[0]; }
From source file:org.apache.nutch.crawl.GeneratorJob2.java
License:Apache License
public int generateBatchId(Configuration conf, String batchId) { MongoClient mongoClient = null;//from w w w .ja v a2 s. com try { mongoClient = new MongoClient(goraMongoAddress); DB db = mongoClient.getDB(goraMongoDb); String cId = conf.get(Nutch.CRAWL_ID_KEY); String collPrefix = ""; if (org.apache.commons.lang3.StringUtils.isNoneEmpty(cId)) { collPrefix = cId + "_"; } String crawlColl = collPrefix + "webpage"; DBCollection collOps = db.getCollection(crawlColl); //update({"count":{$gt:20}},{$set:{"name":"c4"}},false,true) BasicDBObject q = new BasicDBObject("batchId", null); DBObject set = new BasicDBObject("batchId", batchId); set.put("markers._gnmrk_", batchId); BasicDBObject o = new BasicDBObject("$set", set); WriteResult wr = collOps.update(q, o, false, true); long curTime = System.currentTimeMillis(); //taotoxht add q = new BasicDBObject(); q.append("fetchTime", new BasicDBObject().append(QueryOperators.GT, curTime)); o = new BasicDBObject(); o.append("$set", new BasicDBObject().append("fetchTime", curTime)); collOps.update(q, o, false, true); return wr.getN(); } catch (Exception e) { e.printStackTrace(); return 0; } finally { if (mongoClient != null) { mongoClient.close(); } } }
From source file:org.eclipselabs.mongoemf.query.simple.SimpleQueryEngine.java
License:Open Source License
private DBObject buildDBObjectQuery(Expression expression) { final DBObject dbObject = new BasicDBObject(); if (expression != null) { new QuerySwitch<Object>() { Object getValue(Literal literal) { return literal.getValue() == null ? literal.getLiteralValue() : literal.getValue(); }//from ww w.j a v a 2s . c o m @Override public Object caseBinaryOperation(BinaryOperation binaryOperation) { Expression leftOperand = binaryOperation.getLeftOperand(); String operator = binaryOperation.getOperator(); if ("==".equals(operator)) { Expression rightOperand = binaryOperation.getRightOperand(); String property = ExpressionBuilder.toString(leftOperand); if (Keywords.ID_KEY.equals(property)) { dbObject.put(property, new ObjectId(((Literal) rightOperand).getLiteralValue())); } else if (rightOperand instanceof Literal) { dbObject.put(property, getValue((Literal) rightOperand)); } else if ("null".equals(ExpressionBuilder.toString(rightOperand))) { DBObject notExists = new BasicDBObject(); notExists.put("$exists", Boolean.FALSE); dbObject.put(property, notExists); } else { // TODO: What to do? } } else if ("!=".equals(operator)) { Expression rightOperand = binaryOperation.getRightOperand(); String property = ExpressionBuilder.toString(leftOperand); if (rightOperand instanceof Literal) { DBObject notEqual = new BasicDBObject(); notEqual.put("$ne", getValue((Literal) rightOperand)); dbObject.put(property, notEqual); } else if ("null".equals(ExpressionBuilder.toString(rightOperand))) { DBObject exists = new BasicDBObject(); exists.put("$exists", Boolean.TRUE); dbObject.put(property, exists); } else { // TODO: What to do? } } else if ("<".equals(operator) || "<=".equals(operator) || ">".equals(operator) || ">=".equals(operator)) { Expression rightOperand = binaryOperation.getRightOperand(); String property = ExpressionBuilder.toString(leftOperand); if (rightOperand instanceof Literal) { DBObject compare = new BasicDBObject(); compare.put( "<".equals(operator) ? QueryOperators.LT : "<=".equals(operator) ? QueryOperators.LTE : ">".equals(operator) ? QueryOperators.GT : QueryOperators.GTE, getValue((Literal) rightOperand)); dbObject.put(property, compare); } else { // TODO: What to do? } } else if ("||".equals(operator)) { DBObject leftObject = buildDBObjectQuery(leftOperand); DBObject rightObject = buildDBObjectQuery(binaryOperation.getRightOperand()); @SuppressWarnings("unchecked") List<Object> or = (List<Object>) leftObject.get("$or"); if (or != null) { or.add(rightObject); dbObject.putAll(leftObject); } else { or = new ArrayList<Object>(); or.add(leftObject); or.add(rightObject); dbObject.put("$or", or); } } else if ("&&".equals(operator)) { dbObject.putAll(buildDBObjectQuery(leftOperand)); DBObject rightObject = buildDBObjectQuery(binaryOperation.getRightOperand()); for (String field : rightObject.keySet()) { Object rightValue = rightObject.get(field); Object leftValue = dbObject.get(field); if (leftValue instanceof DBObject) { DBObject leftDBObject = (DBObject) leftValue; if (rightValue instanceof DBObject) { DBObject rightDBObject = (DBObject) rightValue; if (leftDBObject.containsField("$nin") && rightDBObject.containsField("$ne")) { @SuppressWarnings("unchecked") List<Object> values = (List<Object>) leftDBObject.get("$nin"); values.add(rightDBObject.get("$ne")); } else if (leftDBObject.containsField("$ne") && rightDBObject.containsField("$ne")) { DBObject nin = new BasicDBObject(); List<Object> values = new ArrayList<Object>(); values.add(leftDBObject.get("$ne")); values.add(rightDBObject.get("$ne")); nin.put("$nin", values); dbObject.put(field, nin); } else { leftDBObject.putAll(rightDBObject); } } else { Object all = leftDBObject.get("$all"); if (all instanceof List<?>) { @SuppressWarnings("unchecked") List<Object> allList = (List<Object>) all; allList.add(rightValue); } else { // What to do? } } } else if (leftValue instanceof List<?>) { @SuppressWarnings("unchecked") List<Object> leftListValue = (List<Object>) leftValue; if (rightValue instanceof List<?>) { leftListValue.addAll((List<?>) rightValue); } else { leftListValue.add(rightValue); } } else if (leftValue != null) { if (rightValue instanceof List<?>) { @SuppressWarnings("unchecked") List<Object> rightListValue = (List<Object>) rightValue; rightListValue.add(0, leftValue); dbObject.put(field, rightListValue); } else { List<Object> listValue = new ArrayList<Object>(); listValue.add(leftValue); listValue.add(rightValue); DBObject in = new BasicDBObject("$all", listValue); dbObject.put(field, in); } } else { dbObject.put(field, rightValue); } } } return null; } }.doSwitch(expression); } return dbObject; }
From source file:org.fornax.cartridges.sculptor.framework.accessimpl.mongodb.MongoDbFindByConditionAccessImpl.java
License:Apache License
protected void makeCriterion(DBObject query, ConditionalCriteria crit, boolean not) { ConditionalCriteria.Operator operator = crit.getOperator(); if (Operator.Equal.equals(operator)) { Object dbValue = toData(crit.getFirstOperant()); if (not) { dbValue = new BasicDBObject(QueryOperators.NE, dbValue); }/*from w w w . j av a2 s. c o m*/ query.put(crit.getPropertyFullName(), dbValue); } else if (Operator.Like.equals(operator)) { Pattern regex = regex(crit.getFirstOperant(), false); Object dbValue = wrapNot(not, regex); query.put(crit.getPropertyFullName(), dbValue); } else if (Operator.IgnoreCaseLike.equals(operator)) { Pattern regex = regex(crit.getFirstOperant(), true); Object dbValue = wrapNot(not, regex); query.put(crit.getPropertyFullName(), dbValue); } else if (Operator.In.equals(operator)) { Object dbValue = toData(crit.getFirstOperant()); if (not) { dbValue = new BasicDBObject(QueryOperators.NIN, dbValue); } else { dbValue = new BasicDBObject(QueryOperators.IN, dbValue); } query.put(crit.getPropertyFullName(), dbValue); } else if (Operator.LessThan.equals(operator)) { Object dbValue = toData(crit.getFirstOperant()); if (not) { dbValue = new BasicDBObject(QueryOperators.GTE, dbValue); } else { dbValue = new BasicDBObject(QueryOperators.LT, dbValue); } query.put(crit.getPropertyFullName(), dbValue); } else if (Operator.LessThanOrEqual.equals(operator)) { Object dbValue = toData(crit.getFirstOperant()); if (not) { dbValue = new BasicDBObject(QueryOperators.GT, dbValue); } else { dbValue = new BasicDBObject(QueryOperators.LTE, dbValue); } query.put(crit.getPropertyFullName(), dbValue); } else if (Operator.GreatThan.equals(operator)) { Object dbValue = toData(crit.getFirstOperant()); if (not) { dbValue = new BasicDBObject(QueryOperators.LTE, dbValue); } else { dbValue = new BasicDBObject(QueryOperators.GT, dbValue); } query.put(crit.getPropertyFullName(), dbValue); } else if (Operator.GreatThanOrEqual.equals(operator)) { Object dbValue = toData(crit.getFirstOperant()); if (not) { dbValue = new BasicDBObject(QueryOperators.LT, dbValue); } else { dbValue = new BasicDBObject(QueryOperators.GTE, dbValue); } query.put(crit.getPropertyFullName(), dbValue); } else if (Operator.IsNull.equals(operator)) { Object dbValue = null; if (not) { dbValue = new BasicDBObject(QueryOperators.NE, dbValue); } query.put(crit.getPropertyFullName(), dbValue); } else if (Operator.IsNotNull.equals(operator)) { Object dbValue; if (not) { dbValue = null; } else { dbValue = new BasicDBObject(QueryOperators.NE, null); } query.put(crit.getPropertyFullName(), dbValue); } else if (Operator.IsEmpty.equals(operator)) { Object dbValue = ""; if (not) { dbValue = new BasicDBObject(QueryOperators.NE, dbValue); } query.put(crit.getPropertyFullName(), dbValue); } else if (Operator.IsNotEmpty.equals(operator)) { Object dbValue; if (not) { dbValue = ""; } else { dbValue = new BasicDBObject(QueryOperators.NE, ""); } query.put(crit.getPropertyFullName(), dbValue); } else if (not && Operator.Between.equals(operator)) { throw new UnsupportedOperationException("Not between condition not supported"); } else if (Operator.Between.equals(operator)) { Object first = toData(crit.getFirstOperant()); Object second = toData(crit.getSecondOperant()); DBObject dbValue = new BasicDBObject(); dbValue.put(QueryOperators.GTE, first); dbValue.put(QueryOperators.LTE, second); query.put(crit.getPropertyFullName(), dbValue); } else if (Operator.And.equals(operator)) { makeCriterion(query, (ConditionalCriteria) crit.getFirstOperant(), not); makeCriterion(query, (ConditionalCriteria) crit.getSecondOperant(), not); } else if (Operator.Not.equals(operator)) { makeCriterion(query, (ConditionalCriteria) crit.getFirstOperant(), !not); } else if (Operator.Or.equals(operator)) { throw new UnsupportedOperationException("Or condition not supported"); } }
From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBQueryBuilder.java
License:Apache License
protected Object pushDownNot(Object object) { if (!(object instanceof DBObject)) { throw new QueryParseException("Cannot do NOT on: " + object); }//from w ww . j a va 2 s . c om DBObject ob = (DBObject) object; Set<String> keySet = ob.keySet(); if (keySet.size() != 1) { throw new QueryParseException("Cannot do NOT on: " + ob); } String key = keySet.iterator().next(); Object value = ob.get(key); if (!key.startsWith("$")) { if (value instanceof DBObject) { // push down inside dbobject return new BasicDBObject(key, pushDownNot(value)); } else { // k = v -> k != v return new BasicDBObject(key, new BasicDBObject(QueryOperators.NE, value)); } } if (QueryOperators.NE.equals(key)) { // NOT k != v -> k = v return value; } if (QueryOperators.NOT.equals(key)) { // NOT NOT v -> v return value; } if (QueryOperators.AND.equals(key) || QueryOperators.OR.equals(key)) { // boolean algebra // NOT (v1 AND v2) -> NOT v1 OR NOT v2 // NOT (v1 OR v2) -> NOT v1 AND NOT v2 String op = QueryOperators.AND.equals(key) ? QueryOperators.OR : QueryOperators.AND; List<Object> list = (List<Object>) value; for (int i = 0; i < list.size(); i++) { list.set(i, pushDownNot(list.get(i))); } return new BasicDBObject(op, list); } if (QueryOperators.IN.equals(key) || QueryOperators.NIN.equals(key)) { // boolean algebra // IN <-> NIN String op = QueryOperators.IN.equals(key) ? QueryOperators.NIN : QueryOperators.IN; return new BasicDBObject(op, value); } if (QueryOperators.LT.equals(key) || QueryOperators.GT.equals(key) || QueryOperators.LTE.equals(key) || QueryOperators.GTE.equals(key)) { // TODO use inverse operators? return new BasicDBObject(QueryOperators.NOT, ob); } throw new QueryParseException("Unknown operator for NOT: " + key); }
From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBQueryBuilder.java
License:Apache License
public DBObject walkGt(Operand lvalue, Operand rvalue) { FieldInfo fieldInfo = walkReference(lvalue); Object right = walkOperand(rvalue); return new FieldInfoDBObject(fieldInfo, new BasicDBObject(QueryOperators.GT, right)); }
From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBQueryBuilder.java
License:Apache License
public DBObject walkBetween(Operand lvalue, Operand rvalue, boolean positive) { LiteralList l = (LiteralList) rvalue; FieldInfo fieldInfo = walkReference(lvalue); Object left = walkOperand(l.get(0)); Object right = walkOperand(l.get(1)); if (positive) { DBObject range = new BasicDBObject(); range.put(QueryOperators.GTE, left); range.put(QueryOperators.LTE, right); return new FieldInfoDBObject(fieldInfo, range); } else {/*w w w. j a v a2s . co m*/ DBObject a = new FieldInfoDBObject(fieldInfo, new BasicDBObject(QueryOperators.LT, left)); DBObject b = new FieldInfoDBObject(fieldInfo, new BasicDBObject(QueryOperators.GT, right)); return new BasicDBObject(QueryOperators.OR, Arrays.asList(a, b)); } }
From source file:org.tinygroup.mongodb.engine.comparemode.impl.BetweenAndCompareMode.java
License:GNU General Public License
public BSONObject generateBSONObject(String propertyName, Object value, Context context) { if (value != null && value.getClass().isArray()) { BSONObject object = new BasicDBObject(); Object[] array = (Object[]) value; if (array.length == 2) { object.put(QueryOperators.GT, array[0]); object.put(QueryOperators.LT, array[1]); return new BasicDBObject(propertyName, object); }// www .ja va2 s . co m } return null; }