Refine search
switch (value.getBsonType()) { case INT32: return value.asInt32().getValue(); case INT64: return value.asInt64().getValue(); case STRING: return value.asString().getValue(); case DECIMAL128: return value.asDecimal128().doubleValue(); case DOUBLE: return value.asDouble().getValue(); 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()); case BINARY: return value.asBinary().getData(); case DATE_TIME: return new Date(value.asDateTime().getValue()); case SYMBOL: return value.asSymbol().getSymbol(); case ARRAY: return value.asArray().toArray(); case DOCUMENT: return Document.parse(value.asDocument().toJson()); default: return value;
/** * 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 the hint for which index to use. The default is not to set a hint. * * @return the hint * @since 3.6 * @mongodb.server.release 3.6 */ public BsonDocument getHint() { if (hint == null) { return null; } if (!hint.isDocument()) { throw new IllegalArgumentException("Hint is not a BsonDocument please use the #getHintBsonValue() method. "); } return hint.asDocument(); }
@Override public ObjectId getObjectId() { if (!fileId.isObjectId()) { throw new MongoGridFSException("Custom id type used for this GridFS upload stream"); } return fileId.asObjectId().getValue(); }
private boolean hasFilterOnPassword(BsonDocument filters) { if (filters == null || filters.isEmpty()) { return false; } else { if (filters.isDocument()) { return filters.keySet().contains(propNamePassword) || filters .keySet().stream() .filter(key -> filters.get(key).isDocument()) .map(key -> filters.get(key).asDocument()) .anyMatch(doc -> hasFilterOnPassword(doc)); } else { return true; } } }
Stream<Value> valuesFrom(BsonValue bsonValue) throws BsonInvalidOperationException { BsonDocument doc = bsonValue.asDocument(); return deserialiserMap.values().stream() .map(d -> d.apply(doc)) .filter(Optional::isPresent) .map(Optional::get); }
/** * @param vars RequestContext.getAggregationVars() * @return the stages, with unescaped operators and bound variables * @throws org.restheart.handlers.metadata.InvalidMetadataException * @throws org.restheart.handlers.aggregation.QueryVariableNotBoundException */ public List<BsonDocument> getResolvedStagesAsList(BsonDocument vars) throws InvalidMetadataException, QueryVariableNotBoundException { BsonArray replacedStages = bindAggregationVariables( JsonUtils.unescapeKeys(stages), vars) .asArray(); List<BsonDocument> ret = new ArrayList<>(); replacedStages.stream().filter((stage) -> (stage.isDocument())) .forEach((stage) -> { ret.add(stage.asDocument()); }); return ret; }
/** * @see https://docs.mongodb.com/manual/reference/operator/update/ * * @param ignoreCurrentDate true to ignore $currentDate * @param json * @return true if json contains update operators */ public static boolean containsUpdateOperators(BsonValue json, boolean ignoreCurrentDate) { if (json == null) { return false; } else if (json.isDocument()) { return _containsUpdateOperators(json.asDocument(), ignoreCurrentDate); } else if (json.isArray()) { return json.asArray() .stream() .filter(el -> el.isDocument()) .anyMatch(element -> _containsUpdateOperators(element.asDocument(), ignoreCurrentDate)); } else { return false; } }
/** * checks if the aggregation variable start with $ this is not allowed since * the client would be able to modify the aggregation stages * * @param aVars RequestContext.getAggregationVars() */ public static void checkAggregationVariables(BsonValue aVars) throws SecurityException { if (aVars == null) { return; } if (aVars.isDocument()) { BsonDocument _obj = aVars.asDocument(); _obj.forEach((key, value) -> { if (key.startsWith("$")) { throw new SecurityException( "aggregation variables cannot include operators"); } if (value.isDocument() || value.isArray()) { checkAggregationVariables(value); } }); } else if (aVars.isArray()) { aVars.asArray().getValues().stream() .filter(el -> (el.isDocument() || el.isArray())) .forEachOrdered(AbstractAggregationOperation::checkAggregationVariables); } }
= mdb.getCollection(collName, BsonDocument.class); BsonObjectId newEtag = new BsonObjectId(new ObjectId()); .stream() .filter(d -> d != null && d.isDocument()) .forEachOrdered(document -> { document. asDocument() .put("_etag", newEtag); });
@Override void applyGlobalTransformers(HttpServerExchange exchange, RequestContext context) { // execture global request tranformers getGlobalTransformers().stream() .filter(gt -> doesGlobalTransformerAppy(gt, exchange, context)) .forEachOrdered(gt -> { if (context.getContent() == null || context.getContent().isDocument()) { gt.transform( exchange, context, context.getContent()); } else if (context.getContent().isArray()) { context.getContent().asArray().forEach(doc -> { gt.transform( exchange, context, doc); }); } }); }
public static void unescapeSchema(BsonDocument schema) { BsonValue unescaped = JsonUtils.unescapeKeys(schema); if (unescaped != null && unescaped.isDocument()) { List<String> keys = Lists.newArrayList(schema.keySet().iterator()); keys.stream().forEach(f -> schema.remove(f)); schema.putAll(unescaped.asDocument()); } }
public void convertFieldValue(Entry<String, BsonValue> keyvalueforStruct, Struct struct, Schema schema) { Object colValue = null; String key = keyvalueforStruct.getKey(); BsonType type = keyvalueforStruct.getValue().getBsonType(); colValue = keyvalueforStruct.getValue().asString().getValue().toString(); break; colValue = keyvalueforStruct.getValue().asObjectId().getValue().toString(); break; colValue = keyvalueforStruct.getValue().asDouble().getValue(); break; colValue = keyvalueforStruct.getValue().asBinary().getData(); break; colValue = keyvalueforStruct.getValue().asInt32().getValue(); break; colValue = keyvalueforStruct.getValue().asInt64().getValue(); break; colValue = keyvalueforStruct.getValue().asBoolean().getValue(); break; colValue = keyvalueforStruct.getValue().asDateTime().getValue(); break; colValue = keyvalueforStruct.getValue().asJavaScript().getCode();
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())); } }); } }
private void _transform(BsonDocument data, Set<String> propertiesNames) { data.keySet().stream().forEach(key -> { BsonValue value = data.get(key); if (shouldTransform(key, propertiesNames)) { if (value.isString() && ObjectId.isValid(value .asString() .getValue())) { data.put(key, new BsonObjectId( new ObjectId(value .asString() .getValue()))); } } if (value instanceof BsonDocument) { _transform(value.asDocument(), propertiesNames); } }); }
private void transformWrite( BsonValue contentToTransform, final BsonDocument responseContent) { if (contentToTransform.isDocument()) { if (contentToTransform.asDocument().containsKey("_embedded") && contentToTransform.asDocument().get("_embedded") .isDocument() && contentToTransform.asDocument().get("_embedded") .asDocument().containsKey("rh:result") && contentToTransform.asDocument().get("_embedded") .asDocument().get("rh:result").isArray()) { BsonArray bulkResp = contentToTransform.asDocument() .get("_embedded").asDocument().get("rh:result") .asArray(); if (bulkResp.size() > 0) { BsonValue el = bulkResp.get(0); if (el.isDocument()) { BsonDocument doc = el.asDocument(); doc .keySet() .stream() .forEach(key -> responseContent .append(key, doc.get(key))); } } } } }
private static void embeddedDocuments( List<BsonDocument> embeddedData, String requestPath, Representation rep, boolean isHalFull) { embeddedData.stream().forEach((d) -> { BsonValue _id = d.get("_id"); if (_id != null && (_id.isString() || _id.isObjectId())) { Representation nrep = new Representation(); if (isHalFull) { nrep.addProperty("_type", new BsonString(RequestContext.TYPE.INDEX.name())); } nrep.addProperties(d); rep.addRepresentation("rh:index", nrep); } else { rep.addWarning("index with _id " + _id + (_id == null ? " " : " of type " + _id.getBsonType().name()) + "filtered out. Indexes can only " + "have ids of type String"); LOGGER.debug("index missing string _id field", d); } }); }