@Override public BsonValue getDocumentId(final BsonDocument document) { return document.get(ID_FIELD_NAME); } }
private static WriteConcernResult createWriteResult(final BsonDocument result) { BsonBoolean updatedExisting = result.getBoolean("updatedExisting", BsonBoolean.FALSE); return WriteConcernResult.acknowledged(result.getNumber("n", new BsonInt32(0)).intValue(), updatedExisting.getValue(), result.get("upserted")); }
private static <T> QueryResult<T> cursorDocumentToQueryResult(final BsonDocument cursorDocument, final ServerAddress serverAddress, final String fieldNameContainingBatch) { long cursorId = ((BsonInt64) cursorDocument.get("id")).getValue(); MongoNamespace queryResultNamespace = new MongoNamespace(cursorDocument.getString("ns").getValue()); return new QueryResult<T>(queryResultNamespace, BsonDocumentWrapperHelper.<T>toList(cursorDocument, fieldNameContainingBatch), cursorId, serverAddress); }
private static WriteConcernError getWriteConcernError(final BsonDocument result) { BsonDocument writeConcernErrorDocument = (BsonDocument) result.get("writeConcernError"); if (writeConcernErrorDocument == null) { return null; } else { return createWriteConcernError(writeConcernErrorDocument); } }
/** * Gets the value of the key if it is a BsonDocument, or throws if not. * * @param key the key * @return the value of the key as a BsonDocument * @throws org.bson.BsonInvalidOperationException if the document does not contain the key or the value is not a BsonDocument */ public BsonDocument getDocument(final Object key) { throwIfKeyAbsent(key); return get(key).asDocument(); }
/** * Gets the value of the key if it is a BsonDouble, or throws if not. * * @param key the key * @return the value of the key as a BsonDouble * @throws org.bson.BsonInvalidOperationException if the document does not contain the key or the value is not of the expected type */ public BsonDouble getDouble(final Object key) { throwIfKeyAbsent(key); return get(key).asDouble(); }
/** * Returns true if the value of the key is a BsonNumber, returns false if the document does not contain the key. * * @param key the key * @return true if the value of the key is a BsonNumber, returns false if the document does not contain the key. */ public boolean isNumber(final Object key) { if (!containsKey(key)) { return false; } return get(key).isNumber(); }
/** * Returns true if the value of the key is a BsonInt32, returns false if the document does not contain the key. * * @param key the key * @return true if the value of the key is a BsonInt32, returns false if the document does not contain the key. */ public boolean isInt32(final Object key) { if (!containsKey(key)) { return false; } return get(key).isInt32(); }
/** * Returns true if the value of the key is a BsonInt64, returns false if the document does not contain the key. * * @param key the key * @return true if the value of the key is a BsonInt64, returns false if the document does not contain the key. */ public boolean isInt64(final Object key) { if (!containsKey(key)) { return false; } return get(key).isInt64(); }
/** * Returns true if the value of the key is a BsonTimestamp, returns false if the document does not contain the key. * * @param key the key * @return true if the value of the key is a BsonTimestamp, returns false if the document does not contain the key. */ public boolean isTimestamp(final Object key) { if (!containsKey(key)) { return false; } return get(key).isTimestamp(); }
/** * Gets the value of the key if it is a BsonObjectId, or throws if not. * * @param key the key * @return the value of the key as a BsonObjectId * @throws org.bson.BsonInvalidOperationException if the document does not contain the key or the value is not of the expected type */ public BsonObjectId getObjectId(final Object key) { throwIfKeyAbsent(key); return get(key).asObjectId(); }
/** * Gets the value of the key if it is a BsonRegularExpression, or throws if not. * * @param key the key * @return the value of the key as a BsonRegularExpression * @throws org.bson.BsonInvalidOperationException if the document does not contain the key or the value is not of the expected type */ public BsonRegularExpression getRegularExpression(final Object key) { throwIfKeyAbsent(key); return get(key).asRegularExpression(); }
/** * Returns true if the value of the key is a BsonNull, returns false if the document does not contain the key. * * @param key the key * @return true if the value of the key is a BsonNull, returns false if the document does not contain the key. */ public boolean isNull(final Object key) { if (!containsKey(key)) { return false; } return get(key).isNull(); }
/** * Returns true if the value of the key is a BsonBoolean, returns false if the document does not contain the key. * * @param key the key * @return true if the value of the key is a BsonBoolean, returns false if the document does not contain the key. */ public boolean isBoolean(final Object key) { if (!containsKey(key)) { return false; } return get(key).isBoolean(); }
private BsonDocument getInsertDocument(R record, BsonDocument key) { BsonDocument valueDocument = BsonDocument.parse(record.value().toString()); valueDocument.remove("_id"); valueDocument.append("id", key.get("id")); return valueDocument; }
@Override public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) { BsonDocument compoundIndex = new BsonDocument(); for (Bson index : indexes) { BsonDocument indexDocument = index.toBsonDocument(documentClass, codecRegistry); for (String key : indexDocument.keySet()) { compoundIndex.append(key, indexDocument.get(key)); } } return compoundIndex; } };
@Override public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) { BsonDocument combinedDocument = new BsonDocument(); for (Bson sort : projections) { BsonDocument sortDocument = sort.toBsonDocument(documentClass, codecRegistry); for (String key : sortDocument.keySet()) { combinedDocument.remove(key); combinedDocument.append(key, sortDocument.get(key)); } } return combinedDocument; }
@Test public void write() throws Exception { JsonObject obj = new JsonObject(); BigInteger bigInteger = new BigInteger(Long.toString(Long.MAX_VALUE)).multiply(new BigInteger("128")); obj.addProperty("bigInteger", bigInteger); BigDecimal bigDecimal = new BigDecimal(Long.MAX_VALUE).multiply(new BigDecimal(1024)); obj.addProperty("bigDecimal", bigDecimal); BsonDocument doc = Jsons.toBson(obj); check(doc.get("bigInteger").getBsonType()).is(BsonType.DECIMAL128); check(doc.get("bigInteger").asDecimal128().decimal128Value().bigDecimalValue().toBigInteger()).is(bigInteger); check(doc.get("bigDecimal").getBsonType()).is(BsonType.DECIMAL128); check(doc.get("bigDecimal").asDecimal128().decimal128Value().bigDecimalValue()).is(bigDecimal); }
@Test public void bigNumbers() throws Exception { JsonObject obj = new JsonObject(); BigInteger bigInteger = new BigInteger(Long.toString(Long.MAX_VALUE)).multiply(new BigInteger("128")); obj.addProperty("bigInteger", bigInteger); BigDecimal bigDecimal = new BigDecimal(Long.MAX_VALUE).multiply(new BigDecimal(1024)); obj.addProperty("bigDecimal", bigDecimal); BsonDocument bson = Jsons.toBson(obj); check(bson.get("bigInteger").getBsonType()).is(BsonType.DECIMAL128); check(bson.get("bigInteger").asDecimal128().decimal128Value().bigDecimalValue().toBigInteger()).is(bigInteger); check(bson.get("bigDecimal").getBsonType()).is(BsonType.DECIMAL128); check(bson.get("bigDecimal").asDecimal128().decimal128Value().bigDecimalValue()).is(bigDecimal); check(Jsons.toGson(bson)).is(obj); } }
BsonValue toBson() { BsonDocument bson = new BsonDocument(); org.bson.BsonWriter writer = new BsonDocumentWriter(bson); // Bson doesn't allow to write directly scalars / primitives, they have to be embedded in a // document. writer.writeStartDocument(); writer.writeName("$"); encoder.encode(writer, value, EncoderContext.builder().build()); writer.writeEndDocument(); writer.flush(); return bson.get("$"); }