Java Code Examples for org.bson.Document#getDouble()

The following examples show how to use org.bson.Document#getDouble() . 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: MongoAdminService.java    From cloudfoundry-service-broker with Apache License 2.0 6 votes vote down vote up
private void addDbOwnerRole(String databaseName){
	MongoDatabase db = client.getDatabase(adminDatabase);
	Map<String, Object> roles = new BasicDBObject();
	roles.put("role", "dbOwner");
	roles.put("db", databaseName);
	
	Map<String, Object> commandArguments = new BasicDBObject();
    commandArguments.put("grantRolesToUser", adminUsername);
    commandArguments.put("roles", Arrays.asList(roles));
    BasicDBObject grantRolesToUserCmd = new BasicDBObject(commandArguments);
    
    Document result = db.runCommand(grantRolesToUserCmd);
	if (result.getDouble("ok") != 1.0d) {
		throw handleException(new MongoServiceException(result.toString()));
	}
}
 
Example 2
Source File: MongoAdminService.java    From cloudfoundry-service-broker with Apache License 2.0 6 votes vote down vote up
public void createUser(String database, String username, String password) throws MongoServiceException {
	try {
		MongoDatabase db = client.getDatabase(database);
		Map<String, Object> roles = new BasicDBObject();
		roles.put("role", "readWrite");
		roles.put("db", database);

		Map<String, Object> commandArguments = new BasicDBObject();
	    commandArguments.put("createUser", username);
	    commandArguments.put("pwd", password);
	   
	    commandArguments.put("roles", Arrays.asList(roles));
	    BasicDBObject createUserCmd = new BasicDBObject(commandArguments);
		
		
		Document result = db.runCommand(createUserCmd);
		if (result.getDouble("ok") != 1.0d) {
			throw handleException(new MongoServiceException(result.toString()));
		}
	} catch (MongoException e) {
		throw handleException(e);
	}
}
 
Example 3
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 合统计
 *
 * @param collectionName
 * @param match
 * @param sumField
 * @return
 */
public Double sum(String collectionName, Document match, String sumField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.sum("_sum", "$" + sumField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getDouble("_sum");
    }
    return null;
}
 
Example 4
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 平均统计
 *
 * @param collectionName
 * @param match
 * @param avgField
 * @return
 */
public Double avg(String collectionName, Document match, String avgField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.avg("_avg", "$" + avgField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getDouble("_avg");
    }
    return null;
}
 
Example 5
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 标准差统计
 *
 * @param collectionName
 * @param match
 * @param stdDevField
 * @return
 */
public Double stdDevPop(String collectionName, Document match, String stdDevField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.stdDevPop("_stdDev", "$" + stdDevField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getDouble("_stdDev");
    }
    return null;
}
 
Example 6
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 采样标准差统计
 *
 * @param collectionName
 * @param match
 * @param stdDevField
 * @param sampleSize
 * @return
 */
public Double stdDevSamp(String collectionName, Document match, String stdDevField, int sampleSize) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , sample(sampleSize)
                    , group(null, Accumulators.stdDevSamp("_stdDev", "$" + stdDevField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getDouble("_stdDev");
    }
    return null;
}
 
Example 7
Source File: ReNounFact.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Construct the fact from a mongo document
 *
 * @param document to construct from
 */
public ReNounFact(Document document) {
  subject = document.getString(SUBJECT_FIELD);
  object = document.getString(OBJECT_FIELD);
  attribute = document.getString(ATTRIBUTE_FIELD);
  score = document.getDouble(SCORE_FIELD);
  pattern = document.getString(PATTERN_FIELD);
  sentence = document.getString(SENTENCE_FIELD);
}
 
Example 8
Source File: ScoredPattern.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Construct the Scored pattern from the given document
 *
 * @param document (mongo) to construct from
 */
public ScoredPattern(Document document) {

  pattern = document.getString(PATTERN_FACT_FIELD);
  frequency = document.getInteger(FREQUENCY_KEY);
  coherence = document.getDouble(COHERENCE_KEY);
}
 
Example 9
Source File: MongoAdminService.java    From cloudfoundry-service-broker with Apache License 2.0 5 votes vote down vote up
public void deleteUser(String database, String username) throws MongoServiceException {
	try {
		MongoDatabase db = client.getDatabase(database);
		Document result = db.runCommand(new BasicDBObject("dropUser", username));
		if (result.getDouble("ok") != 1.0d) {
			throw handleException(new MongoServiceException(result.toString()));
		}
	} catch (MongoException e) {
		throw handleException(e);
	}
}
 
Example 10
Source File: DocumentToStudentTransformer.java    From Java-programming-methodology-Rxjava-articles with Apache License 2.0 4 votes vote down vote up
@Override
public StudentDTO transform(Document source) {
    return new StudentDTO(source.getString(NAME), source.getInteger(AGE), source.getDouble(CREDIT),
            source.getString(MAJOR), ((ObjectId) source.get(ID)).toString());
}
 
Example 11
Source File: DocumentToStudentTransformer.java    From Java-programming-methodology-Rxjava-articles with Apache License 2.0 4 votes vote down vote up
@Override
public StudentDTO transform(Document source) {
    return new StudentDTO(source.getString(NAME), source.getInteger(AGE), source.getDouble(CREDIT),
            source.getString(MAJOR), ((ObjectId) source.get(ID)).toString());
}
 
Example 12
Source File: DocumentToStudentTransformer.java    From Java-9-Spring-Webflux with Apache License 2.0 4 votes vote down vote up
@Override
public StudentDTO transform(Document source) {
    return new StudentDTO(source.getString(NAME), source.getInteger(AGE), source.getDouble(CREDIT),
            source.getString(MAJOR), ((ObjectId) source.get(ID)).toString());
}
 
Example 13
Source File: DocumentToStudentTransformer.java    From Java-9-Spring-Webflux with Apache License 2.0 4 votes vote down vote up
@Override
public StudentDTO transform(Document source) {
    return new StudentDTO(source.getString(NAME), source.getInteger(AGE), source.getDouble(CREDIT),
            source.getString(MAJOR), ((ObjectId) source.get(ID)).toString());
}
 
Example 14
Source File: RenderDao.java    From render with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return list of section data objects for the specified stackId.
 *
 * @throws IllegalArgumentException
 *   if any required parameters are missing or the stack section data has not been built.
 *
 * @throws ObjectNotFoundException
 *   if the stack cannot be found.
 */
public List<SectionData> getSectionData(final StackId stackId,
                                        final Double minZ,
                                        final Double maxZ)
        throws IllegalArgumentException, ObjectNotFoundException {

    MongoUtil.validateRequiredParameter("stackId", stackId);

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

    if (! MongoUtil.exists(renderDatabase, stackId.getSectionCollectionName())) {
        throwExceptionIfStackIsMissing(stackId);
        throw new IllegalArgumentException("section data not aggregated for " + stackId +
                                           ", set stack state to COMPLETE to generate the aggregate collection");
    }

    final MongoCollection<Document> sectionCollection = getSectionCollection(stackId);

    final Document query = new Document();
    if (minZ != null) {
        if (maxZ != null) {
            query.append("_id.z", new Document(QueryOperators.GTE, minZ).append(QueryOperators.LTE, maxZ));
        } else {
            query.append("_id.z", new Document(QueryOperators.GTE, minZ));
        }
    } else if (maxZ != null) {
        query.append("_id.z", new Document(QueryOperators.LTE, maxZ));
    }

    try (final MongoCursor<Document> cursor = sectionCollection.find(query).iterator()) {
        Document document;
        Document resultId;
        String sectionId;
        Number tileCount;
        Long tileCountLong = null;
        Double z;
        Double minX;
        Double maxX;
        Double minY;
        Double maxY;
        while (cursor.hasNext()) {
            document = cursor.next();
            resultId = document.get("_id", Document.class);
            sectionId = resultId.get("sectionId", String.class);
            z = resultId.get("z", Double.class);

            // Need to convert tileCount to long this way because aggregation seems
            // to create an Integer if the tileCount is small enough.
            // Using a long "just in case" there is a section with more than 2^31-1 tiles.
            tileCount = document.get("tileCount", Number.class);
            if (tileCount != null) {
                tileCountLong = tileCount.longValue();
            }

            minX = document.getDouble("minX");
            maxX = document.getDouble("maxX");
            minY = document.getDouble("minY");
            maxY = document.getDouble("maxY");

            if ((sectionId != null) && (z != null)) {
                list.add(new SectionData(sectionId, z, tileCountLong, minX, maxX, minY, maxY));
            }
        }
    }

    LOG.debug("getSectionData: returning {} values for {}.find({})",
              list.size(), sectionCollection.getNamespace().getFullName());

    return list;
}