Open mojoboss opened 8 years ago
I would like to say that I got the exact same error just now. Is the database being initialized properly?
Note: I understand this is an old issue, people still use this workshopper though, so...
@mojoboss This is not a bug, there is a lot going on in this lesson and it's easy to miss something, your solution has a couple of issues.
First, your $group
object has a weird structure, you wrote (I formatted it to make it easier to see the difference):
{
$group: {_id: "average"},
average: {$avg: "$price"}
}
but it should be:
{
$group: {
_id: "average",
average: { $avg: "$price" }
}
}
Second, the parameter of the aggregate
function should be an array of objects, while you passed two objects. What you did:
collection.aggregate(
{ $match: { ... }},
{ $group: { ... }}
)
The correct way (the objects are now elements of an array, notice the square brackets):
collection.aggregate([
{ $match: { ... }},
{ $group: { ... }}
])
There you have it! After these fixes everything works correctly on my machine. @evanlucas I think you can close this issue.
This is my solution for the aggregate exercise, the problem is its giving this error-
var size = process.argv[2];
var url = "mongodb://localhost:27017/learnyoumongo";
var mongo = require("mongodb").MongoClient;
mongo.connect(url, function(err, db){
if(err) throw err;
var collection = db.collection("prices");
collection.aggregate({$match: {"size": size}}, {$group: {_id: "average"}, average:{$avg: "$price"
}}).toArray(function(err, results){
if(err) throw err;
console.log(Number(results[0].average).toFixed(2));
db.close();
});
});
error-
I'm not even using .remove(), but it still gives the same error. I used the official solution but still getting the same error.