mongo-pipebuilder 0.2.1__py3-none-any.whl → 0.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mongo_pipebuilder/__init__.py +1 -1
- mongo_pipebuilder/builder.py +22 -11
- {mongo_pipebuilder-0.2.1.dist-info → mongo_pipebuilder-0.2.2.dist-info}/METADATA +2 -1
- mongo_pipebuilder-0.2.2.dist-info/RECORD +7 -0
- {mongo_pipebuilder-0.2.1.dist-info → mongo_pipebuilder-0.2.2.dist-info}/licenses/LICENSE +1 -0
- mongo_pipebuilder-0.2.1.dist-info/RECORD +0 -7
- {mongo_pipebuilder-0.2.1.dist-info → mongo_pipebuilder-0.2.2.dist-info}/WHEEL +0 -0
- {mongo_pipebuilder-0.2.1.dist-info → mongo_pipebuilder-0.2.2.dist-info}/top_level.txt +0 -0
mongo_pipebuilder/__init__.py
CHANGED
mongo_pipebuilder/builder.py
CHANGED
|
@@ -158,36 +158,47 @@ class PipelineBuilder:
|
|
|
158
158
|
self._stages.append({"$project": fields})
|
|
159
159
|
return self
|
|
160
160
|
|
|
161
|
-
def group(self, group_by: Dict[str, Any], accumulators: Dict[str, Any]) -> Self:
|
|
161
|
+
def group(self, group_by: Union[str, Dict[str, Any], Any], accumulators: Dict[str, Any]) -> Self:
|
|
162
162
|
"""
|
|
163
163
|
Add a $group stage for grouping documents.
|
|
164
164
|
|
|
165
165
|
Args:
|
|
166
|
-
group_by: Expression for grouping (becomes _id)
|
|
166
|
+
group_by: Expression for grouping (becomes _id). Can be:
|
|
167
|
+
- A string (field path, e.g., "$category")
|
|
168
|
+
- A dict (composite key, e.g., {"category": "$category"})
|
|
169
|
+
- Any other value (null, number, etc.)
|
|
167
170
|
accumulators: Dictionary with accumulators (sum, avg, count, etc.)
|
|
168
171
|
|
|
169
172
|
Returns:
|
|
170
173
|
Self for method chaining
|
|
171
174
|
|
|
172
175
|
Raises:
|
|
173
|
-
TypeError: If
|
|
174
|
-
ValueError: If both group_by and accumulators are empty
|
|
176
|
+
TypeError: If accumulators is not a dictionary
|
|
177
|
+
ValueError: If both group_by and accumulators are empty (when group_by is dict/str)
|
|
175
178
|
|
|
176
179
|
Example:
|
|
177
180
|
>>> builder.group(
|
|
178
|
-
... group_by=
|
|
181
|
+
... group_by="$category", # String field path
|
|
182
|
+
... accumulators={"total": {"$sum": "$amount"}}
|
|
183
|
+
... )
|
|
184
|
+
>>> builder.group(
|
|
185
|
+
... group_by={"category": "$category"}, # Composite key
|
|
179
186
|
... accumulators={"total": {"$sum": "$amount"}}
|
|
180
187
|
... )
|
|
181
188
|
"""
|
|
182
|
-
if not isinstance(group_by, dict):
|
|
183
|
-
raise TypeError(f"group_by must be a dict, got {type(group_by)}")
|
|
184
189
|
if not isinstance(accumulators, dict):
|
|
185
190
|
raise TypeError(f"accumulators must be a dict, got {type(accumulators)}")
|
|
186
191
|
|
|
187
|
-
#
|
|
188
|
-
#
|
|
189
|
-
if
|
|
190
|
-
|
|
192
|
+
# Validate empty cases
|
|
193
|
+
# group_by can be None, empty string, empty dict, etc. - all are valid in MongoDB
|
|
194
|
+
# But if it's a string and empty, or dict and empty, and accumulators is also empty,
|
|
195
|
+
# it's likely an error
|
|
196
|
+
if isinstance(group_by, dict):
|
|
197
|
+
if not group_by and not accumulators:
|
|
198
|
+
raise ValueError("group_by and accumulators cannot both be empty")
|
|
199
|
+
elif isinstance(group_by, str):
|
|
200
|
+
if not group_by and not accumulators:
|
|
201
|
+
raise ValueError("group_by and accumulators cannot both be empty")
|
|
191
202
|
|
|
192
203
|
group_stage = {"_id": group_by, **accumulators}
|
|
193
204
|
self._stages.append({"$group": group_stage})
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mongo-pipebuilder
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: Type-safe, fluent MongoDB aggregation pipeline builder
|
|
5
5
|
Author-email: seligoroff <seligoroff@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -373,3 +373,4 @@ See [DEVELOPMENT.md](DEVELOPMENT.md) for development guidelines.
|
|
|
373
373
|
|
|
374
374
|
MIT License - see [LICENSE](LICENSE) file for details.
|
|
375
375
|
|
|
376
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
mongo_pipebuilder/__init__.py,sha256=VF9G-Gp0kabZMoEmeQnSnSqHT3hLqxlEHY-FBeXfnVA,336
|
|
2
|
+
mongo_pipebuilder/builder.py,sha256=qvyQd1k9YbaIV0tRixQnsNr7yuE-_SSGN5tBSwDAk5E,27199
|
|
3
|
+
mongo_pipebuilder-0.2.2.dist-info/licenses/LICENSE,sha256=xAHmf48PmIziXYIdaJzRYeYpXFUPIb70SsSPhAHdggY,1089
|
|
4
|
+
mongo_pipebuilder-0.2.2.dist-info/METADATA,sha256=QZJAl4akVEiT9ucIaviwAwJuvTWFL9XyBEyKb_LI6jc,9127
|
|
5
|
+
mongo_pipebuilder-0.2.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
6
|
+
mongo_pipebuilder-0.2.2.dist-info/top_level.txt,sha256=wLn7H_v-qaNIws5FeBbKPZBCmYFYgFEhPaLjoCWcisc,18
|
|
7
|
+
mongo_pipebuilder-0.2.2.dist-info/RECORD,,
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
mongo_pipebuilder/__init__.py,sha256=eOj_NuBMA9YbVFPmm13UV25RR5_QK2ctrx8QXV3yGTU,336
|
|
2
|
-
mongo_pipebuilder/builder.py,sha256=kurxcQ5IXErefUQjqQ5XAzkfZEv9siN-PufmWoFA0aE,26545
|
|
3
|
-
mongo_pipebuilder-0.2.1.dist-info/licenses/LICENSE,sha256=GLx_6hrvLsyIL34dpRYvjCSIXyYD8PzhBR09opTrixI,1088
|
|
4
|
-
mongo_pipebuilder-0.2.1.dist-info/METADATA,sha256=Kjav6gd0U9MREeLM3tcLz2b5RfBlCyjnJP0H9gG97XM,9126
|
|
5
|
-
mongo_pipebuilder-0.2.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
6
|
-
mongo_pipebuilder-0.2.1.dist-info/top_level.txt,sha256=wLn7H_v-qaNIws5FeBbKPZBCmYFYgFEhPaLjoCWcisc,18
|
|
7
|
-
mongo_pipebuilder-0.2.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|