meteorhacks/meteor-aggregate

Not working with the easiest pipeline

Closed this issue · 2 comments

For collection of documents:

{ 
    "_id" : "Et8fYufWoaJwHFY3d", 
    "date" : ISODate("2016-03-03T21:00:00.000+0000"), 
    "group" : {
        "_id" : "fsa5S555zCiJDdxFY", 
        "name" : "Group"
    }, 
    "trainer" : {
        "_id" : "cuZsxTWLWvD7Nqwe7", 
        "name" : "Trainer"
    }, 
    "clients" : [
        {
            "_id" : "JEbz2bXs3d8BDQSSA", 
            "name" : "Client1"
        }, 
        {
            "_id" : "S5N8oJMarDMvievSs", 
            "name" : "Client2", 
            "came" : true
        }
    ]
}

Following

Collection.aggregate(
          {$unwind: '$clients'},
          {$match: {'clients.came': true}},
          {$project: {date: 1, group: '$group.name', trainer: '$trainer.name', client: '$clients.name'}}
        );

Would give us collection of wrong documents

{
          _id: 'Et8fYufWoaJwHFY3d',
          date: ISODate("2016-03-03T21:00:00.000+0000"),
          "group": {
            "_id": "fsa5S555zCiJDdxFY",
            "name": "Group"
          },
          "trainer": {
            "_id": "cuZsxTWLWvD7Nqwe7",
            "name": "Trainer"
          },
          clients: {
            "_id": "S5N8oJMarDMvievSs",
            "name": "Client2",
            came: true
          }
        }

instead of what mongo would give us and what it should be:

{
          _id: 'Et8fYufWoaJwHFY3d',
          date: ISODate("2016-03-03T21:00:00.000+0000"),
          "group": "Group",
          "trainer": "Trainer",
          "client": "Client2"
        }
ovidb commented

I think you should wrap your pipeline in square brackets like this:

    Collection.aggregate([
      {$unwind: '$clients'},
      {$match: {'clients.came': true}},
      {$project: {date: 1, group: '$group.name', trainer: '$trainer.name', client: '$clients.name'}}
    ]);

It would be a charm! But it was an issue in a finished project. I'll close for now untill I try it out some day. Thx.