Randy Hall
Randy Hall

Reputation: 8137

Aggregate returns empty array

Using mongoose's Model.aggregate() returns an empty array.

I've essentially copied the format as seen here.

var match = {};
var project = {};
    project["_id"] = 0;
    project["products.totalprice"] = 1;
    project["line"] = "$products.closedate";

ThisCollection.aggregate([
    {$match: match},
    {$project: project},
    {$group: {
        _id: "$line"
    }}

], function(err, docs){
    console.log(docs); //!! Returning []
});

My schema is essentially a name and _id field with a nested array of products with totalprice, closedate and some other fields.

There are most certainly a plethora of entries (some 130,000 records). Can anyone spot an issue with this?

Upvotes: 2

Views: 2210

Answers (1)

Alex
Alex

Reputation: 21766

I have created this dummy data to represent a skeleton of your schema:

db.data.save({name:"a",products:{totalprice:1,closedate:1}})
db.data.save({name:"b",products:{totalprice:2,closedate:2}})

This query does return two records and is identical to yours when inserting the JSON string for the JavaScript variable project

ThisCollection.aggregate([{$match:{}},{$project:{"_id":0,"products.totalprice":1,line:"$products.closedate"}},{$group:{_id:"$line"}}])

Upvotes: 1

Related Questions