adonis-mongodb icon indicating copy to clipboard operation
adonis-mongodb copied to clipboard

Is it possible to create a view from an aggregation?

Open maasencioh opened this issue 2 years ago • 1 comments

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');
  }
}

maasencioh avatar Apr 25 '22 10:04 maasencioh

Sounds useful. Would you like to do a PR?

targos avatar Apr 25 '22 12:04 targos