聚合组多个_id和访问权限

时间:2017-01-29 23:32:25

标签: javascript angular typescript mongoose aggregate

我想用Angular2和* ngFor显示一个集合。

我尝试了类似*ngFor ="let x in data" {{x._id.category}}的内容,但它没有用。

enter image description here

如何显示此对象或有没有办法使用mongoose?

gquery= [{
    $group: {
        _id: { category: '$category', specCategory: '$specCategory' },
        total:{ $sum:1 }
    }
}];

Experiences.aggregate(gquery, function (err, results) {    
    callback(err, results || {min: 0, max: 0});
});

HTML

<div class="col-sm-3">
    <select class="guest categorydd" [(ngModel)]="category_search" [ngModelOptions]="{standalone: true}" (ngModelChange)="changeCategory($event)">
        <option value="Alle Kategorien" >All Categories</option>
        <option *ngFor="let x of countedCategory " value="{{x._id}}"> {{x._id}} ({{x.total}})</option>
    </select>
</div>

组件文件

countedCategory: any[];
this.countedCategory = data[0].priceBoundaries;

1 个答案:

答案 0 :(得分:0)

据我所知,你应该尝试这样的事情:

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>

    <div class="col-sm-3">
      <select class="guest categorydd" [(ngModel)]="category_search" [ngModelOptions]="{standalone: true}" (ngModelChange)="changeCategory($event)">
        <option value="Alle Kategorien" >All Categories</option>
        <option
          *ngFor="let x of countedCategory"
          [value]="x._id.category + '.' + x._id.specCategory">
          {{x._id.category}}: {{x._id.specCategory}} ({{x.total}})
        </option>
      </select>
    </div>
  `,
})
export class App {
  name:string;

  category_search;
  countedCategory = [
    {
      _id: { category: 'reisen', specCategory: 'romantik' },
      total: 100
    },
    {
      _id: { category: 'reisen', specCategory: 'abenteuer' },
      total: 77
    },
    {
      _id: { category: 'reisen', specCategory: 'essen' },
      total: 186
    }
  ];

  constructor() {
    this.name = 'Angular2'
  }

  private changeCategory(event: any) {
    console.log(event);
  }
}

您无法将对象用作value它。这将导致[object object]

您需要一个唯一键或创建一个,如上例所示。

live-demo:https://plnkr.co/edit/uy5VS9cOt73qY37kQ00X?p=preview