const stream = torrentdb.collection('log').aggregate([
findDocByAggregation(coll, query, cb) { if(!query.length){ throw Error("mongoClient.findDocByAggregation: query is not an object"); } try { this.db.collection(coll).aggregate(query).toArray(function(err, doc) { if(err) logger.error("mongoClient.findDocByAggregation: query", query, ", err", err); else logger.info("mongoClient.findDocByAggregation: Aggregation successfull."); cb(err, doc); }); } catch(e) { logger.error("mongoClient.findDocByAggregation: Error caught,", e); cb(e, null); } }
MongoClient.connect(dbUrl, function (err, con) { if (err) throw err; const db = con.db('webstore2'); db.collection('orders').aggregate([ { $lookup: { from: 'products', localField: 'prod_id', foreignField: '_id', as: 'orderdetails' } } ]).toArray() .then(res => { res.forEach(order => console.log(JSON.stringify(order))); }).catch(err => { console.log("Error: Update unsuccessfull.") }).finally(() => { con.close(); }) });
MongoClient.connect(dbUrl, function (err, con) { if (err) throw err; const db = con.db('myblog9'); db.collection('posts').aggregate([ { $lookup: { from: 'users', localField: 'author', foreignField: '_id', as: 'authordetails' } } ]).toArray() .then(res => { res.forEach(post => console.log(JSON.stringify(post))); }).catch(err => { console.log("Error: Update unsuccessfull.") }).finally(() => { con.close(); }) });