Skip to content

Commit cf3eb71

Browse files
committed
Use $sample to extract field names
1 parent 79b429a commit cf3eb71

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/Schema/Builder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,24 @@ public function getTableListing()
154154
public function getColumns($table)
155155
{
156156
$stats = $this->connection->getMongoDB()->selectCollection($table)->aggregate([
157+
// Sample 10,000 documents to get a representative sample of the collection
158+
['$sample' => ['size' => 10_000]],
159+
// Convert each document to an array of fields
157160
['$project' => ['fields' => ['$objectToArray' => '$$ROOT']]],
161+
// Unwind to get one document per field
158162
['$unwind' => '$fields'],
163+
// Group by field name, count the number of occurrences and get the types
159164
[
160165
'$group' => [
161166
'_id' => '$fields.k',
162167
'total' => ['$sum' => 1],
163168
'types' => ['$addToSet' => ['$type' => '$fields.v']],
164169
],
165170
],
171+
// Sort by field name
166172
['$sort' => ['_id' => 1]],
173+
// Limit to 1,000 fields
174+
['$limit' => 1000],
167175
], ['typeMap' => ['array' => 'array']])->toArray();
168176

169177
$columns = [];

0 commit comments

Comments
 (0)