Refine search
@Override public Document toDocument(AggregationOperationContext context) { Document condObject = new Document(); condObject.append("if", resolveCriteria(context, condition)); condObject.append("then", resolveValue(context, thenValue)); condObject.append("else", resolveValue(context, otherwiseValue)); return new Document("$cond", condObject); }
@Override public Document toDocument(AggregationOperationContext context) { Document lookupObject = new Document(); lookupObject.append("from", from.getTarget()); lookupObject.append("localField", localField.getTarget()); lookupObject.append("foreignField", foreignField.getTarget()); lookupObject.append("as", as.getTarget()); return new Document("$lookup", lookupObject); }
private void writeChunk() { if (bufferOffset > 0) { if (clientSession != null) { chunksCollection.insertOne(clientSession, new Document("files_id", fileId).append("n", chunkIndex) .append("data", getData())); } else { chunksCollection.insertOne(new Document("files_id", fileId).append("n", chunkIndex).append("data", getData())); } updateMD5(); chunkIndex++; bufferOffset = 0; } }
@Override public Document toDocument() { Document doc = new Document(super.toDocument()); if (length != null) { length.getLowerBound().getValue().ifPresent(it -> doc.append("minLength", it)); length.getUpperBound().getValue().ifPresent(it -> doc.append("maxLength", it)); } if (!StringUtils.isEmpty(pattern)) { doc.append("pattern", pattern); } return doc; }
private Document parseUpdateKey(String updateKey, Map doc) { Document retVal; if (updateKey.equals("_id") && ObjectId.isValid(((String) doc.get(updateKey)))) { retVal = new Document("_id", new ObjectId((String) doc.get(updateKey))); } else if (updateKey.contains(",")) { String[] parts = updateKey.split(",[\\s]*"); retVal = new Document(); for (String part : parts) { retVal.append(part, doc.get(part)); } } else { retVal = new Document(updateKey, doc.get(updateKey)); } return retVal; }
private void createTableMetadata(SchemaTableName schemaTableName, List<MongoColumnHandle> columns) throws TableNotFoundException { String schemaName = schemaTableName.getSchemaName(); String tableName = schemaTableName.getTableName(); MongoDatabase db = client.getDatabase(schemaName); Document metadata = new Document(TABLE_NAME_KEY, tableName); ArrayList<Document> fields = new ArrayList<>(); if (!columns.stream().anyMatch(c -> c.getName().equals("_id"))) { fields.add(new MongoColumnHandle("_id", OBJECT_ID, true).getDocument()); } fields.addAll(columns.stream() .map(MongoColumnHandle::getDocument) .collect(toList())); metadata.append(FIELDS_KEY, fields); MongoCollection<Document> schema = db.getCollection(schemaCollection); schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true)); schema.insertOne(metadata); }
@SuppressWarnings("unchecked") private Document doPrefix(Document source) { Document result = new Document(); for (Map.Entry<String, Object> entry : source.entrySet()) { String key = prefixKey(entry.getKey()); Object value = entry.getValue(); if (entry.getValue() instanceof Collection) { Collection<Object> sourceCollection = (Collection<Object>) entry.getValue(); value = prefixCollection(sourceCollection); } result.append(key, value); } return result; }
private List<Document> guessTableFields(SchemaTableName schemaTableName) { String schemaName = schemaTableName.getSchemaName(); String tableName = schemaTableName.getTableName(); MongoDatabase db = client.getDatabase(schemaName); Document doc = db.getCollection(tableName).find().first(); if (doc == null) { // no records at the collection return ImmutableList.of(); } ImmutableList.Builder<Document> builder = ImmutableList.builder(); for (String key : doc.keySet()) { Object value = doc.get(key); Optional<TypeSignature> fieldType = guessFieldType(value); if (fieldType.isPresent()) { Document metadata = new Document(); metadata.append(FIELDS_NAME_KEY, key); metadata.append(FIELDS_TYPE_KEY, fieldType.get().toString()); metadata.append(FIELDS_HIDDEN_KEY, key.equals("_id") && fieldType.get().equals(OBJECT_ID.getTypeSignature())); builder.add(metadata); } else { log.debug("Unable to guess field type from %s : %s", value == null ? "null" : value.getClass().getName(), value); } } return builder.build(); }