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

Is it possible to create a view from an aggregation? #128

Open
maasencioh opened this issue Apr 25, 2022 · 1 comment
Open

Is it possible to create a view from an aggregation? #128

maasencioh opened this issue Apr 25, 2022 · 1 comment

Comments

@maasencioh
Copy link

maasencioh commented Apr 25, 2022

I want to create a view of a collection; this usually should be done from a migration.

This is an example of what could be like

import BaseMigration from '@ioc:Zakodium/Mongodb/Migration';

export default class TestMigration extends BaseMigration {
  public up(): void {
    this.createView(
      // Name of the view
      'samples.availability',
      // Origin of view
      'samples',
      // Aggregation pipeline
      [
        { $match: { kind: 'sample' } },
        {
          $group: {
            _id: { $first: '$parents' },
            total: { $sum: 1 },
            taken: { $sum: { $cond: ['$meta.reserved', 1, 0] } },
          },
        },
      ],
    );
  }

  public down(): void {
    this.deleteView('samples.availability');
  }
}
@targos
Copy link
Member

targos commented Apr 25, 2022

Sounds useful. Would you like to do a PR?

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

No branches or pull requests

2 participants