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

The following examples show how to use org.bson.BsonDocument#putAll() . 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: Renamer.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
private void doRenaming(final String field, final BsonDocument doc) {
  BsonDocument modifications = new BsonDocument();
  Iterator<Map.Entry<String, BsonValue>> iter = doc.entrySet().iterator();
  while (iter.hasNext()) {
    Map.Entry<String, BsonValue> entry = iter.next();
    String oldKey = entry.getKey();
    BsonValue value = entry.getValue();
    String newKey = renamed(field, oldKey);

    if (!oldKey.equals(newKey)) {
      // IF NEW KEY ALREADY EXISTS WE THEN DON'T RENAME
      // AS IT WOULD CAUSE OTHER DATA TO BE SILENTLY OVERWRITTEN
      // WHICH IS ALMOST NEVER WHAT YOU WANT
      // MAYBE LOG WARNING HERE?
      doc.computeIfAbsent(newKey, k -> modifications.putIfAbsent(k, value));
      iter.remove();
    }
    if (value.isDocument()) {
      String pathToField = field + SUB_FIELD_DOT_SEPARATOR + newKey;
      doRenaming(pathToField, value.asDocument());
    }
  }
  doc.putAll(modifications);
}
 
Example 2
Source File: Renamer.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
protected void doRenaming(String field, BsonDocument doc) {
    Map<String, BsonValue> temp = new LinkedHashMap<>();

    Iterator<Map.Entry<String, BsonValue>> iter = doc.entrySet().iterator();
    while(iter.hasNext()) {
        Map.Entry<String, BsonValue> entry = iter.next();
        String oldKey = entry.getKey();
        BsonValue value = entry.getValue();
        String newKey = renamed(field, oldKey);

        if(!oldKey.equals(newKey)) {
            //IF NEW KEY ALREADY EXISTS WE THEN DON'T RENAME
            //AS IT WOULD CAUSE OTHER DATA TO BE SILENTLY OVERWRITTEN
            //WHICH IS ALMOST NEVER WHAT YOU WANT
            //MAYBE LOG WARNING HERE?
            doc.computeIfAbsent(newKey, k -> temp.putIfAbsent(k,value));
            iter.remove();
        }

        if(value instanceof BsonDocument) {
            String pathToField = field+SUB_FIELD_DOT_SEPARATOR+newKey;
            doRenaming(pathToField, (BsonDocument)value);
        }
    }

    doc.putAll(temp);
}
 
Example 3
Source File: AggregationQuery.java    From immutables with Apache License 2.0 -70 votes vote down vote up
@Override
public void process(Consumer<Bson> consumer) {
  final Function<Collation, Bson> toSortFn = col -> {
    final String name = naming.get(col.path());
    return col.direction().isAscending() ? Sorts.ascending(name) : Sorts.descending(name);

  };

  BsonDocument sort = new BsonDocument();
  for (Collation collation: query.collations()) {
    sort.putAll(toSortFn.apply(collation).toBsonDocument(BsonDocument.class, codecRegistry));
  }

  if (!sort.isEmpty()) {
    consumer.accept(Aggregates.sort(sort));
  }
}