Java Code Examples for org.bson.BsonDocument#remove()

The following examples show how to use org.bson.BsonDocument#remove() . 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: ReplaceOneBusinessKeyStrategy.java    From kafka-connect-mongodb with Apache License 2.0 8 votes vote down vote up
@Override
public WriteModel<BsonDocument> createWriteModel(SinkDocument document) {

    BsonDocument vd = document.getValueDoc().orElseThrow(
            () -> new DataException("error: cannot build the WriteModel since"
                    + " the value document was missing unexpectedly")
    );

    BsonValue businessKey = vd.get(DBCollection.ID_FIELD_NAME);

    if(businessKey == null || !(businessKey instanceof BsonDocument)) {
        throw new DataException("error: cannot build the WriteModel since"
                + " the value document does not contain an _id field of type BsonDocument"
                + " which holds the business key fields");
    }

    vd.remove(DBCollection.ID_FIELD_NAME);

    return new ReplaceOneModel<>((BsonDocument)businessKey, vd, UPDATE_OPTIONS);

}
 
Example 2
Source File: ReplaceOneBusinessKeyStrategy.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
@Override
public WriteModel<BsonDocument> createWriteModel(final SinkDocument document) {
  BsonDocument vd =
      document
          .getValueDoc()
          .orElseThrow(
              () ->
                  new DataException(
                      "Error: cannot build the WriteModel since the value document was missing unexpectedly"));

  try {
    BsonDocument businessKey = vd.getDocument(ID_FIELD);
    vd.remove(ID_FIELD);
    return new ReplaceOneModel<>(businessKey, vd, REPLACE_OPTIONS);
  } catch (BSONException e) {
    throw new DataException(
        "Error: cannot build the WriteModel since the value document does not contain an _id field of"
            + " type BsonDocument which holds the business key fields");
  }
}
 
Example 3
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
static UpdateDescription sanitizeUpdateDescription(
    final @Nullable UpdateDescription updateDescription
) {
  if (updateDescription == null) {
    return null;
  }
  final BsonDocument sanitizedUpdatedFields = updateDescription.getUpdatedFields().clone();
  sanitizedUpdatedFields.remove(DOCUMENT_VERSION_FIELD);

  final ArrayList<String> sanitizedRemovedFields = new ArrayList<>();
  for (final String removedField : updateDescription.getRemovedFields()) {
    if (removedField.equals(DOCUMENT_VERSION_FIELD)) {
      continue;
    }
    sanitizedRemovedFields.add(removedField);
  }

  return new UpdateDescription(
      sanitizedUpdatedFields, sanitizedRemovedFields
  );
}
 
Example 4
Source File: GridFSTest.java    From mongo-java-driver-rx with Apache License 2.0 6 votes vote down vote up
private List<BsonDocument> processFiles(final BsonArray bsonArray, final List<BsonDocument> documents) {
    for (BsonValue rawDocument : bsonArray.getValues()) {
        if (rawDocument.isDocument()) {
            BsonDocument document = rawDocument.asDocument();
            if (document.get("length").isInt32()) {
                document.put("length", new BsonInt64(document.getInt32("length").getValue()));
            }
            if (document.containsKey("metadata") && document.getDocument("metadata").isEmpty()) {
                document.remove("metadata");
            }
            if (document.containsKey("aliases") && document.getArray("aliases").getValues().size() == 0) {
                document.remove("aliases");
            }
            if (document.containsKey("contentType") && document.getString("contentType").getValue().length() == 0) {
                document.remove("contentType");
            }
            documents.add(document);
        }
    }
    return documents;
}
 
Example 5
Source File: GridFSTest.java    From mongo-java-driver-reactivestreams with Apache License 2.0 6 votes vote down vote up
private List<BsonDocument> processFiles(final BsonArray bsonArray, final List<BsonDocument> documents) {
    for (BsonValue rawDocument : bsonArray.getValues()) {
        if (rawDocument.isDocument()) {
            BsonDocument document = rawDocument.asDocument();
            if (document.get("length").isInt32()) {
                document.put("length", new BsonInt64(document.getInt32("length").getValue()));
            }
            if (document.containsKey("metadata") && document.getDocument("metadata").isEmpty()) {
                document.remove("metadata");
            }
            if (document.containsKey("aliases") && document.getArray("aliases").getValues().size() == 0) {
                document.remove("aliases");
            }
            if (document.containsKey("contentType") && document.getString("contentType").getValue().length() == 0) {
                document.remove("contentType");
            }
            documents.add(document);
        }
    }
    return documents;
}
 
Example 6
Source File: UpdateOneBusinessKeyTimestampStrategy.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
@Override
public WriteModel<BsonDocument> createWriteModel(final SinkDocument document) {
  BsonDocument vd =
      document
          .getValueDoc()
          .orElseThrow(
              () ->
                  new DataException(
                      "Error: cannot build the WriteModel since the value document was missing unexpectedly"));

  BsonDateTime dateTime = new BsonDateTime(Instant.now().toEpochMilli());

  try {
    BsonDocument businessKey = vd.getDocument(ID_FIELD);
    vd.remove(ID_FIELD);

    return new UpdateOneModel<>(
        businessKey,
        new BsonDocument("$set", vd.append(FIELD_NAME_MODIFIED_TS, dateTime))
            .append("$setOnInsert", new BsonDocument(FIELD_NAME_INSERTED_TS, dateTime)),
        UPDATE_OPTIONS);

  } catch (BSONException e) {
    throw new DataException(
        "Error: cannot build the WriteModel since the value document does not contain an _id field of"
            + " type BsonDocument which holds the business key fields");
  }
}
 
Example 7
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Given a BSON document, remove any forbidden fields and return the document. If no changes are
 * made, the original document reference is returned. If changes are made, a cloned copy of the
 * document with the changes will be returned.
 *
 * @param document the document from which to remove forbidden fields
 *
 * @return a BsonDocument without any forbidden fields.
 */
static BsonDocument sanitizeDocument(
    final @Nullable BsonDocument document
) {
  if (document == null) {
    return null;
  }
  if (document.containsKey(DOCUMENT_VERSION_FIELD)) {
    final BsonDocument clonedDoc = document.clone();
    clonedDoc.remove(DOCUMENT_VERSION_FIELD);

    return clonedDoc;
  }
  return document;
}
 
Example 8
Source File: MongoCaptureUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
private BsonDocument makeOtherwiseIdenticalFilter(BsonDocument filter) {
	filter.remove("errorDeclaration");
	filter.put("errorDeclaration", new BsonDocument("$exists", new BsonBoolean(false)));
	filter.remove("recordTime");
	return filter;
}
 
Example 9
Source File: Converter.java    From epcis with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve only temporal properties from document
 * 
 * @param bson
 * @return
 */
public static BsonDocument retrieveTemporalProperties(BsonDocument bson) {
	bson.remove(Tokens.ID);
	bson.remove(Tokens.EDGE);
	bson.remove(Tokens.VERTEX);
	bson.remove(Tokens.TIMESTAMP);
	bson.remove(Tokens.INTERVAL);
	bson.remove(Tokens.TYPE);
	return bson;
}
 
Example 10
Source File: MongoCaptureUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
private BsonDocument makeOtherwiseIdenticalFilter(BsonDocument filter) {
	filter.remove("errorDeclaration");
	filter.put("errorDeclaration", new BsonDocument("$exists", new BsonBoolean(false)));
	filter.remove("recordTime");
	return filter;
}
 
Example 11
Source File: BlockListProjector.java    From mongo-kafka with Apache License 2.0 4 votes vote down vote up
@Override
protected void doProjection(final String field, final BsonDocument doc) {
  if (!field.contains(FieldProjector.SUB_FIELD_DOT_SEPARATOR)) {
    if (field.equals(FieldProjector.SINGLE_WILDCARD)
        || field.equals(FieldProjector.DOUBLE_WILDCARD)) {
      handleWildcard(field, "", doc);
      return;
    }

    // NOTE: never try to remove the _id field
    if (!field.equals(ID_FIELD)) {
      doc.remove(field);
    }
    return;
  }

  int dotIdx = field.indexOf(FieldProjector.SUB_FIELD_DOT_SEPARATOR);
  String firstPart = field.substring(0, dotIdx);
  String otherParts = field.length() >= dotIdx ? field.substring(dotIdx + 1) : "";

  if (firstPart.equals(FieldProjector.SINGLE_WILDCARD)
      || firstPart.equals(FieldProjector.DOUBLE_WILDCARD)) {
    handleWildcard(firstPart, otherParts, doc);
    return;
  }

  BsonValue value = doc.get(firstPart);
  if (value != null) {
    if (value.isDocument()) {
      doProjection(otherParts, value.asDocument());
    }
    if (value.isArray()) {
      BsonArray values = value.asArray();
      for (BsonValue v : values.getValues()) {
        if (v != null && v.isDocument()) {
          doProjection(otherParts, v.asDocument());
        }
      }
    }
  }
}
 
Example 12
Source File: BlacklistProjector.java    From kafka-connect-mongodb with Apache License 2.0 4 votes vote down vote up
@Override
protected void doProjection(String field, BsonDocument doc) {

    if(!field.contains(FieldProjector.SUB_FIELD_DOT_SEPARATOR)) {

        if(field.equals(FieldProjector.SINGLE_WILDCARD)
                || field.equals(FieldProjector.DOUBLE_WILDCARD)) {
            handleWildcard(field,"",doc);
            return;
        }

        //NOTE: never try to remove the _id field
        if(!field.equals(DBCollection.ID_FIELD_NAME))
            doc.remove(field);

        return;
    }

    int dotIdx = field.indexOf(FieldProjector.SUB_FIELD_DOT_SEPARATOR);
    String firstPart = field.substring(0,dotIdx);
    String otherParts = field.length() >= dotIdx
                            ? field.substring(dotIdx+1) : "";

    if(firstPart.equals(FieldProjector.SINGLE_WILDCARD)
        || firstPart.equals(FieldProjector.DOUBLE_WILDCARD)) {
        handleWildcard(firstPart,otherParts,doc);
        return;
    }

    BsonValue value = doc.get(firstPart);
    if(value != null) {
        if(value.isDocument()) {
            doProjection(otherParts, (BsonDocument)value);
        }
        if(value.isArray()) {
            BsonArray values = (BsonArray)value;
            for(BsonValue v : values.getValues()) {
                if(v != null && v.isDocument()) {
                    doProjection(otherParts,(BsonDocument)v);
                }
            }
        }
    }

}