Here is the script I have in "script.js"
conn = new Mongo();
db = conn.getDB("learn");
db.contracts.aggregate([
{ $match: { regionCode: '77' } },
{ $unwind: '$products' },
{
$project: {
_id: '$_id',
regNum: '$regNum',
prodName: '$products.name',
prodPrice: '$products.price'
}
},
{ $match: { 'prodName' : 'Water' } }
], {cursor:{}}).result.forEach(printjson);
To run it, I use the command prompt in the following way
mongo script.js >> out.txt
The output in the "out.txt" file shows an error message
TypeError: Cannot call method 'forEach' of undefined at script.js
I face the same issue when running the script from the mongo shell using mongo.exe (using load()).
However, when executing the same aggregate command from Robomongo 0.8.4, I get successful results (3 documents in json format). Any idea why this inconsistency occurs?
Mongodb version 2.6.5