List of usage examples for com.mongodb QueryBuilder and
@SuppressWarnings("unchecked") public QueryBuilder and(final DBObject... ands)
From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java
License:Apache License
private <T extends Document> BulkUpdateResult sendBulkUpdate(Collection<T> collection, java.util.Collection<UpdateOp> updateOps, Map<String, T> oldDocs) { DBCollection dbCollection = getDBCollection(collection); BulkWriteOperation bulk = dbCollection.initializeUnorderedBulkOperation(); String[] bulkIds = new String[updateOps.size()]; int i = 0;/*from w w w. j a v a 2 s. c om*/ for (UpdateOp updateOp : updateOps) { String id = updateOp.getId(); QueryBuilder query = createQueryForUpdate(id, updateOp.getConditions()); T oldDoc = oldDocs.get(id); DBObject update; if (oldDoc == null) { query.and(Document.MOD_COUNT).exists(false); update = createUpdate(updateOp, true); } else { query.and(Document.MOD_COUNT).is(oldDoc.getModCount()); update = createUpdate(updateOp, false); } bulk.find(query.get()).upsert().updateOne(update); bulkIds[i++] = id; } BulkWriteResult bulkResult; Set<String> failedUpdates = new HashSet<String>(); Set<String> upserts = new HashSet<String>(); try { bulkResult = bulk.execute(); } catch (BulkWriteException e) { bulkResult = e.getWriteResult(); for (BulkWriteError err : e.getWriteErrors()) { failedUpdates.add(bulkIds[err.getIndex()]); } } for (BulkWriteUpsert upsert : bulkResult.getUpserts()) { upserts.add(bulkIds[upsert.getIndex()]); } return new BulkUpdateResult(failedUpdates, upserts); }
From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java
License:Apache License
@Nonnull private static QueryBuilder createQueryForUpdate(String key, Map<Key, Condition> conditions) { QueryBuilder query = getByKeyQuery(key); for (Entry<Key, Condition> entry : conditions.entrySet()) { Key k = entry.getKey();/*from www . j a v a 2 s.c o m*/ Condition c = entry.getValue(); switch (c.type) { case EXISTS: query.and(k.toString()).exists(c.value); break; case EQUALS: query.and(k.toString()).is(c.value); break; case NOTEQUALS: query.and(k.toString()).notEquals(c.value); break; } } return query; }
From source file:org.apache.rya.indexing.mongodb.IndexingMongoDBStorageStrategy.java
License:Apache License
public DBObject getQuery(final StatementConstraints contraints) { final QueryBuilder queryBuilder = QueryBuilder.start(); if (contraints.hasSubject()) { queryBuilder.and(new BasicDBObject(SUBJECT, contraints.getSubject().toString())); }//from ww w. ja va2 s. co m if (contraints.hasPredicates()) { final Set<URI> predicates = contraints.getPredicates(); if (predicates.size() > 1) { for (final URI pred : predicates) { final DBObject currentPred = new BasicDBObject(PREDICATE, pred.toString()); queryBuilder.or(currentPred); } } else if (!predicates.isEmpty()) { queryBuilder.and(new BasicDBObject(PREDICATE, predicates.iterator().next().toString())); } } if (contraints.hasContext()) { queryBuilder.and(new BasicDBObject(CONTEXT, contraints.getContext().toString())); } return queryBuilder.get(); }
From source file:org.entcore.workspace.service.impl.DefaultFolderService.java
License:Open Source License
@Override public void list(String path, UserInfos owner, boolean hierarchical, String filter, final Handler<Either<String, JsonArray>> results) { if (owner == null) { results.handle(new Either.Left<String, JsonArray>("workspace.invalid.user")); return;/*from ww w.j a va 2 s . co m*/ } QueryBuilder q = QueryBuilder.start(); if ("shared".equals(filter)) { List<DBObject> groups = new ArrayList<>(); groups.add(QueryBuilder.start("userId").is(owner.getUserId()).get()); for (String gpId : owner.getGroupsIds()) { groups.add(QueryBuilder.start("groupId").is(gpId).get()); } q.put("shared").elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get()); } else { q.put("owner").is(owner.getUserId()); } q.and("file").exists(false).and("application").is(WorkspaceService.WORKSPACE_NAME); if (path != null && !path.trim().isEmpty()) { if (hierarchical) { q = q.put("folder").regex(Pattern.compile("^" + Pattern.quote(path) + "_[^_]+$")); } else { q = q.put("folder").regex(Pattern.compile("^" + Pattern.quote(path) + "_")); } } else if (hierarchical) { q = q.put("folder").regex(Pattern.compile("^[^_]+$")); } mongo.find(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(q), new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> res) { results.handle(Utils.validResults(res)); } }); }
From source file:org.graylog2.alerts.AlertServiceImpl.java
License:Open Source License
@Override public List<Alert> loadRecentOfStream(String streamId, DateTime since) { QueryBuilder qb = QueryBuilder.start("stream_id").is(streamId); if (since != null) { qb.and("triggered_at").greaterThanEquals(since.toDate()); }//from w ww . j a v a2 s . c o m BasicDBObject sort = new BasicDBObject("triggered_at", -1); final List<DBObject> alertObjects = query(AlertImpl.class, qb.get(), sort, AlertImpl.MAX_LIST_COUNT, 0); List<Alert> alerts = Lists.newArrayList(); for (DBObject alertObj : alertObjects) { alerts.add(new AlertImpl(new ObjectId(alertObj.get("_id").toString()), alertObj.toMap())); } return alerts; }
From source file:org.keycloak.authorization.mongo.store.MongoPolicyStore.java
License:Open Source License
@Override public List<Policy> findByResourceServer(Map<String, String[]> attributes, String resourceServerId, int firstResult, int maxResult) { QueryBuilder queryBuilder = new QueryBuilder().and("resourceServerId").is(resourceServerId); attributes.forEach((name, value) -> { if ("permission".equals(name)) { if (Boolean.valueOf(value[0])) { queryBuilder.and("type").in(new String[] { "resource", "scope" }); } else { queryBuilder.and("type").notIn(new String[] { "resource", "scope" }); }// w w w.j a v a 2 s. c om } else if ("id".equals(name)) { queryBuilder.and("_id").in(value); } else { queryBuilder.and(name).regex(Pattern.compile(".*" + value[0] + ".*", Pattern.CASE_INSENSITIVE)); } }); DBObject sort = new BasicDBObject("name", 1); return getMongoStore().loadEntities(PolicyEntity.class, queryBuilder.get(), sort, firstResult, maxResult, invocationContext).stream().map(policy -> findById(policy.getId())).collect(toList()); }
From source file:org.keycloak.authorization.mongo.store.MongoResourceStore.java
License:Open Source License
@Override public List<Resource> findByResourceServer(Map<String, String[]> attributes, String resourceServerId, int firstResult, int maxResult) { QueryBuilder queryBuilder = new QueryBuilder().and("resourceServerId").is(resourceServerId); attributes.forEach((name, value) -> { if ("scope".equals(name)) { queryBuilder.and("scopes").in(value); } else {//w w w .jav a 2 s .c o m queryBuilder.and(name).regex(Pattern.compile(".*" + value[0] + ".*", Pattern.CASE_INSENSITIVE)); } }); DBObject sort = new BasicDBObject("name", 1); return getMongoStore().loadEntities(ResourceEntity.class, queryBuilder.get(), sort, firstResult, maxResult, invocationContext).stream().map(scope -> findById(scope.getId())).collect(toList()); }
From source file:org.keycloak.authorization.mongo.store.MongoScopeStore.java
License:Open Source License
@Override public List<Scope> findByResourceServer(Map<String, String[]> attributes, String resourceServerId, int firstResult, int maxResult) { QueryBuilder queryBuilder = new QueryBuilder().and("resourceServerId").is(resourceServerId); attributes.forEach((name, value) -> { queryBuilder.and(name).regex(Pattern.compile(".*" + value[0] + ".*", Pattern.CASE_INSENSITIVE)); });/*from w w w . j av a 2 s. c o m*/ DBObject sort = new BasicDBObject("name", 1); return getMongoStore().loadEntities(ScopeEntity.class, queryBuilder.get(), sort, firstResult, maxResult, invocationContext).stream().map(scope -> findById(scope.getId())).collect(toList()); }
From source file:org.keycloak.storage.mongo.MongoUserFederatedStorageProvider.java
License:Apache License
@Override public List<String> getUsersByUserAttribute(RealmModel realm, String name, String value) { QueryBuilder queryBuilder = new QueryBuilder().and("realmId").is(realm.getId()); queryBuilder.and("attributes." + name).is(value); List<FederatedUser> users = getMongoStore().loadEntities(FederatedUser.class, queryBuilder.get(), invocationContext);//from www.j a v a 2 s.co m List<String> ids = new LinkedList<>(); for (FederatedUser user : users) ids.add(user.getId()); return ids; }
From source file:org.keycloak.storage.mongo.MongoUserFederatedStorageProvider.java
License:Apache License
@Override public List<String> getMembership(RealmModel realm, GroupModel group, int firstResult, int max) { QueryBuilder queryBuilder = new QueryBuilder().and("realmId").is(realm.getId()); queryBuilder.and("groupIds").is(group.getId()); List<FederatedUser> users = getMongoStore().loadEntities(FederatedUser.class, queryBuilder.get(), null, firstResult, max, invocationContext); List<String> ids = new LinkedList<>(); for (FederatedUser user : users) ids.add(user.getId());//from w w w . j a v a 2 s. co m return ids; }