javascript - aggregation project mongodb output not expected -


i'm coding complex query in javascript. , i'm using aggregation. i'm using 2 tables. invoice, travel

this code.

      invoice.aggregate([         // filter documents invoice of 2016         {           $match: {             executed: {               $gte: startdate,               $lte: enddate             },             "modelholder.name": 'travel'           }         },          // $lookup working alone, not taking input function 1 of aggregate         {           $lookup: {             from: "travels",             localfield: "modelholder.id",             foreignfield: "_id",             as: "datafromtravels"           }         },          // filter date of reservation , date of arrival           $match: { $or:   [     {       'datafromtravels.from.date': {         $gte: departdate, $lte: enddate       }     },     {       'datafromtravels.to.date': {         $lte: arrivaldate       },     }   ]       }     },           {           $limit: 2         }         // 2nd function work on first function output input       ], function (err, result) {         if (err) {           return console.log(('error', err));           //next(err);         } else {           console.log('result', result);          // res.json(result);           return;         }       });        // console.log('invoice  !');       // console.log(invoice._id);       self.resume();     })      .on('error', function (err) {       console.error('error occurred while streaming invoices', err);     })      .on('end', function () {       console.log('successfully displayed invoices');       cb();     }); 

i want have list of cars , count how many times used each of them in period restricted in match functions.

i added this.

    { $group: { "_id": "$datafromtravels.car.platenumber", "count": { $sum: 1 } }             }, 

the car under travel table. , got array of 3 cars. or have hundreds of cars in same period.

how manage this?

thanks suggestions.

when make join query using mongodb. target collection stored array. when make use of lookup

{     $lookup: {       from: "travels",       localfield: "modelholder.id",       foreignfield: "_id",       as: "datafromtravels"    } } 

the output of query like:-

{   "_id": objectid("554dc5e937c1482c491d9d36"),   // invoice data   "datafromtravels": [     // travel data   ] } 

and mongodb aggregation cannot use $match on nested documents. so, in query match

db.invoice.aggregate([   {     $match: {       executed: {         $gte: isodate(startdate),         $lte: isodate(enddate)       },       "modelholder.name": 'travel'     }   },   {     $lookup: {       from: "travels",       localfield: "modelholder.id",       foreignfield: "_id",       as: "datafromtravels"     }   }, {     $unwind: "$datafromtravels"   },{   $match: {     $or: [{       'datafromtravels.from.date': {         $gte: isodate(startdate),         $lte: isodate(enddate)       }     }, {       'datafromtravels.to.date': {         $lte: isodate(startdate)       },     }]   }   }, {     $limit: 2   } ]); 

now, datafromtravels available object & can use $match on from & to properties


Comments

  1. Your blog is in a convincing manner, thanks for sharing such an information with lots of your effort and time
    mongodb online training India
    mongodb online training hyderabad

    ReplyDelete

Post a Comment

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -