@Override protected ObjectId doReadObjectId() { return currentValue.asObjectId().getValue(); }
/** * 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(); }
/** * If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a * BsonObjectId. * * @param key the key * @param defaultValue the default value * @return the value of the key as a BsonObjectId * @throws org.bson.BsonInvalidOperationException if the document contains the key but the value is not of the expected type */ public BsonObjectId getObjectId(final Object key, final BsonObjectId defaultValue) { if (!containsKey(key)) { return defaultValue; } return get(key).asObjectId(); }
@Override public ObjectId getObjectId() { if (!fileId.isObjectId()) { throw new MongoGridFSException("Custom id type used for this GridFS upload stream"); } return fileId.asObjectId().getValue(); }
/** * The {@link ObjectId} for this file. * * Throws a MongoGridFSException if the file id is not an ObjectId. * * @return the id for this file. */ public ObjectId getObjectId() { if (!id.isObjectId()) { throw new MongoGridFSException("Custom id type used for this GridFS file"); } return id.asObjectId().getValue(); }
colValue = keyvalueforStruct.getValue().asObjectId().getValue().toString(); break; list.add(temp); } else if (arrValue.getBsonType() == BsonType.OBJECT_ID && valueType == BsonType.OBJECT_ID) { String temp = arrValue.asObjectId().getValue().toString(); list.add(temp); } else if (arrValue.getBsonType() == BsonType.DOUBLE && valueType == BsonType.DOUBLE) {
@Test public void withDate() { final Date date = new Date(); final ObjectId id = ObjectId.get(); final Jackson expected = ImmutableJackson.builder() .id(id) .prop1("prop1") .prop2("22") .date(new Date(date.getTime())) .build(); repository.insert(expected).getUnchecked(); check(collection.count()).is(1L); final Jackson actual = repository.findAll().fetchAll().getUnchecked().get(0); check(expected).is(actual); final BsonDocument doc = collection.find().first(); check(doc.keySet()).hasContentInAnyOrder("_id", "prop1", "prop2", "date", "uuid"); check(doc.get("date").asDateTime().getValue()).is(date.getTime()); check(doc.get("_id").asObjectId().getValue()).is(id); }
return value.asBoolean().getValue(); case OBJECT_ID: return value.asObjectId().getValue(); case DB_POINTER: return new DBRef(value.asDBPointer().getNamespace(), value.asDBPointer().getId());
break; case OBJECT_ID: writeObjectId(value.asObjectId().getValue()); break; case BOOLEAN:
return value.asBoolean().getValue(); case OBJECT_ID: return value.asObjectId().getValue(); case DB_POINTER: return new DBRef(value.asDBPointer().getNamespace(), value.asDBPointer().getId());
/** * The {@link ObjectId} for this file. * * Throws a MongoGridFSException if the file id is not an ObjectId. * * @return the id for this file. */ public ObjectId getObjectId() { if (!id.isObjectId()) { throw new MongoGridFSException("Custom id type used for this GridFS file"); } return id.asObjectId().getValue(); }
@Override public ObjectId getObjectId() { if (!fileId.isObjectId()) { throw new MongoGridFSException("Custom id type used for this GridFS upload stream"); } return fileId.asObjectId().getValue(); }
@Override public ObjectId getObjectId() { if (!fileId.isObjectId()) { throw new MongoGridFSException("Custom id type used for this GridFS upload stream"); } return fileId.asObjectId().getValue(); }
private void _transform(BsonDocument data) { data.keySet().stream().forEach(key -> { BsonValue value = data.get(key); if (value.isDocument()) { _transform(value.asDocument()); } else if (value.isObjectId()) { data.put(key, new BsonString(value .asObjectId() .getValue() .toString())); } }); } }
public static Object extractValueEx(BsonValue value) { switch (value.getBsonType()) { case DOUBLE: return value.asDouble().getValue(); case STRING: return value.asString().getValue(); case OBJECT_ID: return value.asObjectId().getValue(); case INT32: return value.asInt32().getValue(); case INT64: return value.asInt64().getValue(); case DECIMAL128: return value.asDecimal128().getValue(); case NULL: return null; } throw new IllegalArgumentException("Unsupported ID type: " + value.getClass()); }
private static String getIdAsStringNoBrachets(BsonValue id) throws UnsupportedDocumentIdException { if (id == null) { return null; } else if (id.isString()) { return id.asString().getValue(); } else if (id.isObjectId()) { return id.asObjectId().getValue().toHexString(); } else { return JsonUtils.minify( JsonUtils.toJson(id)); } }
/** * Check generated document id after an upsert type operation, setting the inserted key using given * {@link OperationResult} builder. * @param builder OperationResult builder * @param documentContext Document context * @param configuration Operation configuration * @param upsertedId Optional upserted id value * @param value Original {@link PropertyBox} value */ @SuppressWarnings("unchecked") public static void checkUpsertedKey(OperationResult.Builder builder, MongoDocumentContext<?> documentContext, DatastoreOperationConfiguration configuration, BsonValue upsertedId, PropertyBox value) { if (upsertedId != null) { final ObjectId oid = upsertedId.asObjectId().getValue(); documentContext.getDocumentIdPath().ifPresent(idp -> { final Object idPropertyValue = documentContext.getDocumentIdResolver().decode(oid, idp.getType()); builder.withInsertedKey(idp, idPropertyValue); // check bring back ids if (configuration.hasWriteOption(DefaultWriteOption.BRING_BACK_GENERATED_IDS)) { documentContext.getDocumentIdProperty().ifPresent(idprp -> { if (value.contains(idprp)) { value.setValue((Property<Object>) idprp, idPropertyValue); } }); } }); } }
/** * * @param id * @param quote * @return the String representation of the id */ public static String getIdAsString(BsonValue id, boolean quote) { if (id == null) { return null; } else if (id.isString()) { return quote ? "'" + id.asString().getValue() + "'" : id.asString().getValue(); } else if (id.isObjectId()) { return id.asObjectId().getValue().toString(); } else { return JsonUtils.minify(JsonUtils.toJson(id) .replace("\"", "'")); } }
/** * * @param exchange * @param etag */ public static void injectEtagHeader( HttpServerExchange exchange, Object etag) { if (etag == null) { return; } if (etag instanceof BsonValue) { BsonValue _etag = (BsonValue) etag; if (_etag.isObjectId()) { setETagHeader(exchange, _etag.asObjectId().getValue().toString()); } else if (_etag.isString()) { setETagHeader(exchange, _etag.asString().getValue()); } } else if (etag instanceof ObjectId) { setETagHeader(exchange, etag.toString()); } else if (etag instanceof String) { setETagHeader(exchange, (String) etag); } }
/** * * @param exchange * @param properties */ public static void injectEtagHeader( HttpServerExchange exchange, BsonDocument properties) { if (properties == null) { return; } BsonValue _etag = properties.get("_etag"); if (_etag == null) { return; } if (_etag.isObjectId()) { setETagHeader(exchange, _etag.asObjectId().getValue().toString()); } else if (_etag.isString()) { setETagHeader(exchange, _etag.asString().getValue()); } }