Java Code Examples for com.mongodb.client.MongoCollection#distinct()

The following examples show how to use com.mongodb.client.MongoCollection#distinct() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RenderDao.java    From render with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return list of stack projects for the specified owner.
 *
 * @throws IllegalArgumentException
 *   if required parameters are not specified.
 */
public List<String> getProjects(final String owner)
        throws IllegalArgumentException {

    MongoUtil.validateRequiredParameter("owner", owner);

    final List<String> list = new ArrayList<>();

    final MongoCollection<Document> stackMetaDataCollection = getStackMetaDataCollection();
    final Document query = new Document("stackId.owner", owner);
    for (final String project : stackMetaDataCollection.distinct("stackId.project", query, String.class)) {
        list.add(project);
    }

    return list;
}
 
Example 2
Source File: RenderDao.java    From render with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param  minZ  if specified (not null), only include z values greater than or equal to this minimum.
 * @param  maxZ  if specified (not null), only include z values less than or equal to this maximum.
 *
 * @return list of distinct z values (layers) for the specified stackId.
 *
 * @throws IllegalArgumentException
 *   if any required parameters are missing or the stack cannot be found.
 */
public List<Double> getZValues(final StackId stackId,
                               final Double minZ,
                               final Double maxZ)
        throws IllegalArgumentException {

    MongoUtil.validateRequiredParameter("stackId", stackId);

    final MongoCollection<Document> tileCollection = getTileCollection(stackId);

    final List<Double> list = new ArrayList<>();
    for (final Double zValue : tileCollection.distinct("z", Double.class)) {
        if (zValue != null) {
            if (minZ == null) {
                if (maxZ == null) {
                    list.add(zValue);
                } else if (zValue <= maxZ) {
                    list.add(zValue);
                }
            } else if (maxZ == null) {
                if (zValue >= minZ) {
                    list.add(zValue);
                }
            } else if ((zValue >= minZ) && (zValue <= maxZ)) {
                list.add(zValue);
            }
        }
    }

    LOG.debug("getZValues: returning {} values between {} and {} for {}",
              list.size(), minZ, maxZ, MongoUtil.fullName(tileCollection));

    return list;
}
 
Example 3
Source File: RenderDao.java    From render with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return list of stack owners.
 *
 * @throws IllegalArgumentException
 *   if required parameters are not specified.
 */
public List<String> getOwners()
        throws IllegalArgumentException {

    final List<String> list = new ArrayList<>();

    final MongoCollection<Document> stackMetaDataCollection = getStackMetaDataCollection();
    for (final String owner : stackMetaDataCollection.distinct("stackId.owner", String.class)) {
        list.add(owner);
    }

    return list;
}
 
Example 4
Source File: QueryConverter.java    From sql-to-mongo-db-query-converter with Apache License 2.0 4 votes vote down vote up
/**
 * @param mongoDatabase the database to run the query against.
 * @param <T> variable based on the type of query run.
 * @return When query does a find will return QueryResultIterator&lt;{@link org.bson.Document}&gt;
 *           When query does a count will return a Long
 *           When query does a distinct will return QueryResultIterator&lt;{@link java.lang.String}&gt;
 * @throws ParseException when the sql query cannot be parsed
 */
@SuppressWarnings("unchecked")
public <T> T run(MongoDatabase mongoDatabase) throws ParseException {
    MongoDBQueryHolder mongoDBQueryHolder = getMongoQuery();

    MongoCollection mongoCollection = mongoDatabase.getCollection(mongoDBQueryHolder.getCollection());

    if (SQLCommandType.SELECT.equals(mongoDBQueryHolder.getSqlCommandType())) {
    	
        if (mongoDBQueryHolder.isDistinct()) {
            return (T) new QueryResultIterator<>(mongoCollection.distinct(getDistinctFieldName(mongoDBQueryHolder), mongoDBQueryHolder.getQuery(), String.class));
        } else if (sqlCommandInfoHolder.isCountAll() && !isAggregate(mongoDBQueryHolder)) {
            return (T) Long.valueOf(mongoCollection.count(mongoDBQueryHolder.getQuery()));
        } else if (isAggregate(mongoDBQueryHolder)) {
            
            AggregateIterable aggregate = mongoCollection.aggregate(generateAggSteps(mongoDBQueryHolder,sqlCommandInfoHolder));

            if (aggregationAllowDiskUse != null) {
                aggregate.allowDiskUse(aggregationAllowDiskUse);
            }

            if (aggregationBatchSize != null) {
                aggregate.batchSize(aggregationBatchSize);
            }

            return (T) new QueryResultIterator<>(aggregate);
        } else {
            FindIterable findIterable = mongoCollection.find(mongoDBQueryHolder.getQuery()).projection(mongoDBQueryHolder.getProjection());
            if (mongoDBQueryHolder.getSort() != null && mongoDBQueryHolder.getSort().size() > 0) {
                findIterable.sort(mongoDBQueryHolder.getSort());
            }
            if (mongoDBQueryHolder.getOffset() != -1) {
                findIterable.skip((int) mongoDBQueryHolder.getOffset());
            }
            if (mongoDBQueryHolder.getLimit() != -1) {
                findIterable.limit((int) mongoDBQueryHolder.getLimit());
            }

            return (T) new QueryResultIterator<>(findIterable);
        }
    } else if (SQLCommandType.DELETE.equals(mongoDBQueryHolder.getSqlCommandType())) {
        DeleteResult deleteResult = mongoCollection.deleteMany(mongoDBQueryHolder.getQuery());
        return (T)((Long)deleteResult.getDeletedCount());
    } else {
        throw new UnsupportedOperationException("SQL command type not supported");
    }
}
 
Example 5
Source File: MongoDbPluginIT.java    From glowroot with Apache License 2.0 4 votes vote down vote up
@Override
public void transactionMarker() {
    MongoDatabase database = mongoClient.getDatabase("testdb");
    MongoCollection<Document> collection = database.getCollection("test");
    collection.distinct("abc", String.class);
}