zakodium / adonis-mongodb

MongoDB provider for AdonisJS 5
https://zakodium.github.io/adonis-mongodb/
Other
63 stars 12 forks source link

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

Open maasencioh opened 2 years ago

maasencioh commented 2 years ago

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 commented 2 years ago

Sounds useful. Would you like to do a PR?