use of io.trino.plugin.mongodb.ObjectIdType.OBJECT_ID in project trino by trinodb.
the class MongoSession method createTableMetadata.
private void createTableMetadata(SchemaTableName schemaTableName, List<MongoColumnHandle> columns, Optional<String> tableComment) {
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, Optional.empty()).getDocument());
}
fields.addAll(columns.stream().map(MongoColumnHandle::getDocument).collect(toList()));
metadata.append(FIELDS_KEY, fields);
tableComment.ifPresent(comment -> metadata.append(COMMENT_KEY, comment));
MongoCollection<Document> schema = db.getCollection(schemaCollection);
if (!indexExists(schema)) {
schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true));
}
schema.insertOne(metadata);
}
Aggregations