* the name of the collection
* @return a table definition for mongo db.
*/
public static SimpleTableDef detectTable(DB db, String collectionName) {
final DBCollection collection = db.getCollection(collectionName);
final DBCursor cursor = collection.find().limit(1000);
final SortedMap<String, Set<Class<?>>> columnsAndTypes = new TreeMap<String, Set<Class<?>>>();
while (cursor.hasNext()) {
DBObject object = cursor.next();
Set<String> keysInObject = object.keySet();
for (String key : keysInObject) {
Set<Class<?>> types = columnsAndTypes.get(key);
if (types == null) {
types = new HashSet<Class<?>>();
columnsAndTypes.put(key, types);
}
Object value = object.get(key);
if (value != null) {
types.add(value.getClass());
}
}
}
cursor.close();
final String[] columnNames = new String[columnsAndTypes.size()];
final ColumnType[] columnTypes = new ColumnType[columnsAndTypes.size()];
int i = 0;