Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregate builder stage .group #268

Open
NotesClef opened this issue Oct 22, 2021 · 2 comments
Open

Aggregate builder stage .group #268

NotesClef opened this issue Oct 22, 2021 · 2 comments

Comments

@NotesClef
Copy link

NotesClef commented Oct 22, 2021

Post on Medium from 2017, as well as comments here on GitHub, indicate that there should be .group available as an aggregate builder stage?

With latest build of MongoKitten, I am receiving an error stating that AggregateBuilderStage has no member of "group" (as per example below):

let aggregateBuilderStages: [AggregateBuilderStage] = [
        AggregateBuilderStage.match(["gender": "female"]),
        AggregateBuilderStage.group(["_id": ["$avg": "$age"]])
        ]

Is group supported currently for aggregate stages, and if so could you please let me know the correct syntax?

Thank you for your help!

@Joannis
Copy link
Member

Joannis commented Oct 23, 2021

We used to have Group, but that operation is so incredibly varied and complicated that we couldn't get a good API for it.

MongoKitten is currently designed with a couple of layers. The most obvious types contain the common and basic operations in a nice API, the Commands API contains anything less common you need to customise, without cluttering the main namespace. And finally you can drop down to raw MongoDB commands.

For Aggregate stages that means that you've got helpers for creating common stages such as match, with helpers for simple queries. For any 'unsupported' stages you can still create a raw AggegateBuilderStage using its initializer.

@RobertoMachorro
Copy link

@NotesClef I had the same issue just now and defined the stage in a Document.

var userGroup = Document()
userGroup["$group"]["_id"] = "null"
userGroup["$group"]["users"]["$sum"] = 1

var userProject = Document()
userProject["$project"]["_id"] = 0

let aggregateBuilderStages: [AggregateBuilderStage] = [
	AggregateBuilderStage(document: userGroup),
	AggregateBuilderStage(document: userProject)
]

Note: Yes, sample above is the same as $count(field).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants