List of usage examples for com.mongodb QueryOperators IN
String IN
To view the source code for com.mongodb QueryOperators IN.
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. ja va 2s. c o 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:org.fornax.cartridges.sculptor.framework.accessimpl.mongodb.MongoDbChunkFetcher.java
License:Apache License
@Override protected List<T> getChunk(Collection keys) { DBObject query = new BasicDBObject(); DBObject inCondition = new BasicDBObject(); Collection restrictionPropertyValues = restrictionPropertyValues(keys); inCondition.put(QueryOperators.IN, restrictionPropertyValues); query.put(getRestrictionPropertyName(), inCondition); DBCursor cur = dbCollection.find(query); List<T> foundResult = new ArrayList<T>(); for (DBObject each : cur) { T eachResult = dataMapper.toDomain(each); foundResult.add(eachResult);// ww w . jav a 2 s .c om } return foundResult; }
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); }// www . j a v 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 v a2 s . c o m 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 walkIn(Operand lvalue, Operand rvalue, boolean positive) { FieldInfo fieldInfo = walkReference(lvalue); Object right = walkOperand(rvalue); if (!(right instanceof List)) { throw new QueryParseException("Invalid IN, right hand side must be a list: " + rvalue); }//w w w . j av a 2s . c o m if (isMixinTypes(fieldInfo)) { return walkMixinTypes((List<String>) right, positive); } // TODO check list fields List<Object> list = (List<Object>) right; return new FieldInfoDBObject(fieldInfo, new BasicDBObject(positive ? QueryOperators.IN : QueryOperators.NIN, list)); }
From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBQueryBuilder.java
License:Apache License
/** * Matches the mixin types against a list of values. * <p>/* w w w .j ava 2 s. com*/ * Used for: * <ul> * <li>ecm:mixinTypes = 'Foo' * <li>ecm:mixinTypes != 'Foo' * <li>ecm:mixinTypes IN ('Foo', 'Bar') * <li>ecm:mixinTypes NOT IN ('Foo', 'Bar') * </ul> * <p> * ecm:mixinTypes IN ('Foo', 'Bar') * * <pre> * { "$or" : [ { "ecm:primaryType" : { "$in" : [ ... types with Foo or Bar ...]}} , * { "ecm:mixinTypes" : { "$in" : [ "Foo" , "Bar]}}]} * </pre> * * ecm:mixinTypes NOT IN ('Foo', 'Bar') * <p> * * <pre> * { "$and" : [ { "ecm:primaryType" : { "$in" : [ ... types without Foo nor Bar ...]}} , * { "ecm:mixinTypes" : { "$nin" : [ "Foo" , "Bar]}}]} * </pre> */ public DBObject walkMixinTypes(List<String> mixins, boolean include) { /* * Primary types that match. */ Set<String> matchPrimaryTypes; if (include) { matchPrimaryTypes = new HashSet<>(); for (String mixin : mixins) { matchPrimaryTypes.addAll(getMixinDocumentTypes(mixin)); } } else { matchPrimaryTypes = new HashSet<>(getDocumentTypes()); for (String mixin : mixins) { matchPrimaryTypes.removeAll(getMixinDocumentTypes(mixin)); } } /* * Instance mixins that match. */ Set<String> matchMixinTypes = new HashSet<>(); for (String mixin : mixins) { if (!isNeverPerInstanceMixin(mixin)) { matchMixinTypes.add(mixin); } } /* * MongoDB query generation. */ // match on primary type DBObject p = new BasicDBObject(DBSDocument.KEY_PRIMARY_TYPE, new BasicDBObject(QueryOperators.IN, matchPrimaryTypes)); // match on mixin types // $in/$nin with an array matches if any/no element of the array matches String innin = include ? QueryOperators.IN : QueryOperators.NIN; DBObject m = new BasicDBObject(DBSDocument.KEY_MIXIN_TYPES, new BasicDBObject(innin, matchMixinTypes)); // and/or between those String op = include ? QueryOperators.OR : QueryOperators.AND; return new BasicDBObject(op, Arrays.asList(p, m)); }
From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBRepository.java
License:Apache License
@Override public List<State> readStates(List<String> ids) { DBObject query = new BasicDBObject(idKey, new BasicDBObject(QueryOperators.IN, ids)); return findAll(query, ids.size()); }
From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBRepository.java
License:Apache License
@Override public void deleteStates(Set<String> ids) { DBObject query = new BasicDBObject(idKey, new BasicDBObject(QueryOperators.IN, ids)); if (log.isTraceEnabled()) { log.trace("MongoDB: REMOVE " + ids); }/*from w w w .j a v a2 s .c o m*/ WriteResult w = coll.remove(query); if (w.getN() != ids.size()) { log.error("Removed " + w.getN() + " docs for " + ids.size() + " ids: " + ids); } }
From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBRepository.java
License:Apache License
protected void addPrincipals(DBObject query, Set<String> principals) { if (principals != null) { DBObject inPrincipals = new BasicDBObject(QueryOperators.IN, new ArrayList<>(principals)); query.put(DBSDocument.KEY_READ_ACL, inPrincipals); }// w w w . j a va 2 s . com }
From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBRepository.java
License:Apache License
@Override public Lock removeLock(String id, String owner) { DBObject query = new BasicDBObject(idKey, id); if (owner != null) { // remove if owner matches or null // implements LockManager.canLockBeRemoved inside MongoDB Object ownerOrNull = Arrays.asList(owner, null); query.put(KEY_LOCK_OWNER, new BasicDBObject(QueryOperators.IN, ownerOrNull)); } // else unconditional remove // remove the lock DBObject old = coll.findAndModify(query, null, null, false, UNSET_LOCK_UPDATE, false, false); if (old != null) { // found a doc and removed the lock, return previous lock String oldOwner = (String) old.get(KEY_LOCK_OWNER); if (oldOwner == null) { // was not locked return null; } else {// w ww. j a v a 2s .c om // return previous lock Calendar oldCreated = (Calendar) converter.scalarToSerializable(old.get(KEY_LOCK_CREATED)); return new Lock(oldOwner, oldCreated); } } else { // doc not found, or lock owner didn't match // get the old lock if (log.isTraceEnabled()) { logQuery(id, LOCK_FIELDS); } old = coll.findOne(new BasicDBObject(idKey, id), LOCK_FIELDS); if (old == null) { // document not found throw new DocumentNotFoundException(id); } String oldOwner = (String) old.get(KEY_LOCK_OWNER); Calendar oldCreated = (Calendar) converter.scalarToSerializable(old.get(KEY_LOCK_CREATED)); if (oldOwner != null) { if (!LockManager.canLockBeRemoved(oldOwner, owner)) { // existing mismatched lock, flag failure return new Lock(oldOwner, oldCreated, true); } // old owner should have matched -- there was a race condition // TODO do better throw new ConcurrentUpdateException("Unlock " + id); } // old owner null, should have matched -- there was a race condition // TODO do better throw new ConcurrentUpdateException("Unlock " + id); } }