2

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?

5
  • Does the query without the $group statement return any results ? Commented Nov 12, 2015 at 22:47
  • @Jaco Nope, tried that still empty. Checking over my keys again, but the " missing was a typo on the copy-paste. Commented Nov 12, 2015 at 22:53
  • @Jaco Almost... Using $products.totalprice in the project object, I'm now getting undefined for docs. Commented Nov 12, 2015 at 23:05
  • @Jaco Hold the phone... might be something wrong with my data. Give me a moment. Commented Nov 12, 2015 at 23:07
  • Please show your document with the expected result. Commented Nov 12, 2015 at 23:11

1 Answer 1

1

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"}}])
Sign up to request clarification or add additional context in comments.

2 Comments

My collection was empty.
Yes, that doesn't help:-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.