private static Bson createNearFilterDocument(final String fieldName, final double x, final double y, @Nullable final Double maxDistance, @Nullable final Double minDistance, final String operator) { BsonDocument nearFilter = new BsonDocument(operator, new BsonArray(Arrays.asList(new BsonDouble(x), new BsonDouble(y)))); if (maxDistance != null) { nearFilter.append("$maxDistance", new BsonDouble(maxDistance)); } if (minDistance != null) { nearFilter.append("$minDistance", new BsonDouble(minDistance)); } return new BsonDocument(fieldName, nearFilter); }
@Override public BsonDouble decode(final BsonReader reader, final DecoderContext decoderContext) { return new BsonDouble(reader.readDouble()); }
/** * Creates a filter that matches all documents containing a field with grid coordinates data that exist entirely within the specified * circle. * * @param fieldName the field name * @param x the x coordinate of the circle * @param y the y coordinate of the circle * @param radius the radius of the circle, as measured in the units used by the coordinate system * @return the filter * @mongodb.driver.manual reference/operator/query/geoWithin/ $geoWithin * @mongodb.driver.manual reference/operator/query/center/#op._S_center $center * @mongodb.server.release 2.4 * @since 3.1 */ public static Bson geoWithinCenter(final String fieldName, final double x, final double y, final double radius) { BsonDocument center = new BsonDocument("$center", new BsonArray(Arrays.<BsonValue>asList(new BsonArray(asList(new BsonDouble(x), new BsonDouble(y))), new BsonDouble(radius)))); return new OperatorFilter<BsonDocument>("$geoWithin", fieldName, center); }
@Override protected void doWriteDouble(final double value) { write(new BsonDouble(value)); }
/** * Creates a filter that matches all documents containing a field with geospatial data (GeoJSON or legacy coordinate pairs) that exist * entirely within the specified circle, using spherical geometry. If using longitude and latitude, specify longitude first. * * @param fieldName the field name * @param x the x coordinate of the circle * @param y the y coordinate of the circle * @param radius the radius of the circle, in radians * @return the filter * @mongodb.driver.manual reference/operator/query/geoWithin/ $geoWithin * @mongodb.driver.manual reference/operator/query/centerSphere/#op._S_centerSphere $centerSphere * @mongodb.server.release 2.4 * @since 3.1 */ public static Bson geoWithinCenterSphere(final String fieldName, final double x, final double y, final double radius) { BsonDocument centerSphere = new BsonDocument("$centerSphere", new BsonArray(Arrays.<BsonValue>asList(new BsonArray(asList(new BsonDouble(x), new BsonDouble(y))), new BsonDouble(radius)))); return new OperatorFilter<BsonDocument>("$geoWithin", fieldName, centerSphere); }
/** * Creates a filter that matches all documents containing a field with grid coordinates data that exist entirely within the specified * box. * * @param fieldName the field name * @param lowerLeftX the lower left x coordinate of the box * @param lowerLeftY the lower left y coordinate of the box * @param upperRightX the upper left x coordinate of the box * @param upperRightY the upper left y coordinate of the box * @return the filter * @mongodb.driver.manual reference/operator/query/geoWithin/ $geoWithin * @mongodb.driver.manual reference/operator/query/box/#op._S_box $box * @mongodb.server.release 2.4 * @since 3.1 */ public static Bson geoWithinBox(final String fieldName, final double lowerLeftX, final double lowerLeftY, final double upperRightX, final double upperRightY) { BsonDocument box = new BsonDocument("$box", new BsonArray(asList(new BsonArray(asList(new BsonDouble(lowerLeftX), new BsonDouble(lowerLeftY))), new BsonArray(asList(new BsonDouble(upperRightX), new BsonDouble(upperRightY)))))); return new OperatorFilter<BsonDocument>("$geoWithin", fieldName, box); }
/** * Creates a filter that matches all documents containing a field with grid coordinates data that exist entirely within the specified * polygon. * * @param fieldName the field name * @param points a list of pairs of x, y coordinates. Any extra dimensions are ignored * @return the filter * @mongodb.driver.manual reference/operator/query/geoWithin/ $geoWithin * @mongodb.driver.manual reference/operator/query/polygon/#op._S_polygon $polygon * @mongodb.server.release 2.4 * @since 3.1 */ public static Bson geoWithinPolygon(final String fieldName, final List<List<Double>> points) { BsonArray pointsArray = new BsonArray(); for (List<Double> point : points) { pointsArray.add(new BsonArray(asList(new BsonDouble(point.get(0)), new BsonDouble(point.get(1))))); } BsonDocument polygon = new BsonDocument("$polygon", pointsArray); return new OperatorFilter<BsonDocument>("$geoWithin", fieldName, polygon); }
private BsonDocument asCommandResponseDocument() { BsonArray cursorIdArray = new BsonArray(); for (long cursorId : cursors) { cursorIdArray.add(new BsonInt64(cursorId)); } return new BsonDocument("ok", new BsonDouble(1)) .append("cursorsUnknown", cursorIdArray); }
return clazz.cast(new Decimal128((Long) value)); } else if (value instanceof Double) { return clazz.cast(new BsonDouble((Double) value).decimal128Value()); } else if (value instanceof String) { return clazz.cast(Decimal128.parse((String) value));
private BsonDocument asFindCommandResponseDocument(final ResponseBuffers responseBuffers, final QueryResult<T> queryResult, final boolean isExplain) { List<ByteBufBsonDocument> rawResultDocuments = Collections.emptyList(); if (responseBuffers.getReplyHeader().getNumberReturned() > 0) { responseBuffers.reset(); rawResultDocuments = ByteBufBsonDocument.createList(responseBuffers); } if (isExplain) { BsonDocument explainCommandResponseDocument = new BsonDocument("ok", new BsonDouble(1)); explainCommandResponseDocument.putAll(rawResultDocuments.get(0)); return explainCommandResponseDocument; } else { BsonDocument cursorDocument = new BsonDocument("id", queryResult.getCursor() == null ? new BsonInt64(0) : new BsonInt64(queryResult.getCursor().getId())) .append("ns", new BsonString(namespace.getFullName())) .append("firstBatch", new BsonArray(rawResultDocuments)); return new BsonDocument("cursor", cursorDocument) .append("ok", new BsonDouble(1)); } }
/** * Reading from BSON to GSON */ @Test public void bsonToGson() throws Exception { BsonDocument document = new BsonDocument(); document.append("boolean", new BsonBoolean(true)); document.append("int32", new BsonInt32(32)); document.append("int64", new BsonInt64(64)); document.append("double", new BsonDouble(42.42D)); document.append("string", new BsonString("foo")); document.append("null", new BsonNull()); document.append("array", new BsonArray()); document.append("object", new BsonDocument()); JsonElement element = TypeAdapters.JSON_ELEMENT.read(new BsonReader(new BsonDocumentReader(document))); check(element.isJsonObject()); check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().isBoolean()); check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().getAsBoolean()); check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().getAsNumber().intValue()).is(32); check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().getAsNumber().longValue()).is(64L); check(element.getAsJsonObject().get("double").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("double").getAsJsonPrimitive().getAsNumber().doubleValue()).is(42.42D); check(element.getAsJsonObject().get("string").getAsJsonPrimitive().isString()); check(element.getAsJsonObject().get("string").getAsJsonPrimitive().getAsString()).is("foo"); check(element.getAsJsonObject().get("null").isJsonNull()); check(element.getAsJsonObject().get("array").isJsonArray()); check(element.getAsJsonObject().get("object").isJsonObject()); }
private BsonDocument asGetMoreCommandResponseDocument(final QueryResult<T> queryResult, final ResponseBuffers responseBuffers) { List<ByteBufBsonDocument> rawResultDocuments = Collections.emptyList(); if (responseBuffers.getReplyHeader().getNumberReturned() != 0) { responseBuffers.reset(); rawResultDocuments = ByteBufBsonDocument.createList(responseBuffers); } BsonDocument cursorDocument = new BsonDocument("id", queryResult.getCursor() == null ? new BsonInt64(0) : new BsonInt64(queryResult.getCursor().getId())) .append("ns", new BsonString(namespace.getFullName())) .append("nextBatch", new BsonArray(rawResultDocuments)); return new BsonDocument("cursor", cursorDocument) .append("ok", new BsonDouble(1)); }
index.append("min", new BsonDouble(request.getMin())); index.append("max", new BsonDouble(request.getMax())); index.append("bucketSize", new BsonDouble(request.getBucketSize()));
@Override protected BsonArray toCoordinates() { BsonArray coordinates = new BsonArray(); coordinates.add( new BsonDouble( longitude ) ); coordinates.add( new BsonDouble( latitude ) ); return coordinates; }
private MongoVersion calculateVersion() { Document buildInfo = driverClient .getDatabase("admin") .runCommand(new BsonDocument("buildInfo", new BsonDouble(1.0))); String versionString = buildInfo.getString("version"); return MongoVersion.fromMongoString(versionString); }
private MongoVersion calculateVersion() { Document buildInfo = driverClient .getDatabase("admin") .runCommand(new BsonDocument("buildInfo", new BsonDouble(1.0))); String versionString = buildInfo.getString("version"); return MongoVersion.fromMongoString(versionString); }
private BsonDocument asCommandResponseDocument() { BsonArray cursorIdArray = new BsonArray(); for (long cursorId : cursors) { cursorIdArray.add(new BsonInt64(cursorId)); } return new BsonDocument("ok", new BsonDouble(1)) .append("cursorsUnknown", cursorIdArray); }
@Override public BsonValue toBson(Object data) { if(data instanceof BigDecimal) { if(format.equals(Format.DECIMAL128)) return new BsonDecimal128(new Decimal128((BigDecimal)data)); if(format.equals(Format.LEGACYDOUBLE)) return new BsonDouble(((BigDecimal)data).doubleValue()); } throw new DataException("error: decimal conversion not possible when data is" + " of type "+data.getClass().getName() + " and format is "+format); } }
private BsonDocument generateMeter(Meter meter) { BsonDocument document = new BsonDocument(); document.append("count", new BsonInt64(meter.getCount())); document.append("m15_rate", new BsonDouble(meter.getFifteenMinuteRate() * rateFactor)); document.append("m1_rate", new BsonDouble(meter.getOneMinuteRate() * rateFactor)); document.append("m5_rate", new BsonDouble(meter.getFiveMinuteRate() * rateFactor)); document.append("mean_rate", new BsonDouble(meter.getMeanRate() * rateFactor)); document.append("units", new BsonString("calls/" + singularRateUnitString)); return document; }
@BeforeEach public void setupDocumentsToRename() { keyDoc = new BsonDocument("fieldA",new BsonString("my field value")); keyDoc.put("f2",new BsonBoolean(true)); keyDoc.put("subDoc",new BsonDocument("fieldX",new BsonInt32(42))); keyDoc.put("my_field1",new BsonDocument("my_field2",new BsonString("testing rocks!"))); valueDoc = new BsonDocument("abc",new BsonString("my field value")); valueDoc.put("f2",new BsonBoolean(false)); valueDoc.put("subDoc",new BsonDocument("123",new BsonDouble(0.0))); valueDoc.put("foo.foo.foo",new BsonDocument(".blah..blah.",new BsonInt32(23))); }