node.js - nodejs /Mongo - multiple finds not working -


still noob node/mongo , stuck on this.

i have 2 mongo collections, tenants , rent. rent collection has tenant _id in schema. following function searching through active tenants , each of pulling out attributes latest rent document. first part of function populates tenant object results. working good. second .then starts iterate through tenant object pulling out _id use in rent query. (join). issue loop seems iterate through , print _id correctly, second find query seems print out last document in object. not sure why happening

thanks in advance

app.get('/chargerenttest/:date', (req,res) => {  //check date valid var rentchargedate = new date(req.params.date); var tenant = ""; //for each active tenant     tenant .find({            activetenant : true             })          .then ((tenant) => {                     if (!tenant) {              return res.status(404).send();            }            console.log("found tenents")            return tenant           })            .then ((tenant) => {            (var in tenant) {                 console.log(i)                 console.log(tenant[i]._id)                 rent                  .find({                   "paymenttype" :"rent",                   "tenantid" : tenant[i]._id,                   "activeentry": true})                 .limit(1)                 .sort({datepaid: -1})                   // sort in decending date ( latested on top)                 .then ((rent) => {                       lastpayment = rent[0].datepaid;                       lastamountpaid = rent[0].amountpaid;                        console.log("--------",i)                        console.log("tenant",tenant[i]._id)                       console.log("rentamount",tenant[i].rentamount)                       console.log("lastpayment", lastpayment)                              });                         }             }) 

})

your query can simplified running aggregate operation makes use of pipeline $lookup operator allows perform left outer join collection in same database filter in documents "joined" collection processing.

consider running following pipeline:

rent.aggregate([     {         "$match": {             "paymenttype": "rent",             "activeentry": true         }     },     {         "$lookup": {             "from": "tenants",             "localfield": "tenantid",             "foreignfield": "_id",             "as": "tenants"         }     },     { "$match": { "tenants": { "$ne": [] }, "tenants.activetenant": true } },     //{ "$unwind": "$tenants" },     { "$sort": { "datepaid": -1 } },     { "$limit": 1 }      ]).exec((err, rent) => {     if (err) throw err;      lastpayment = rent[0].datepaid;     lastamountpaid = rent[0].amountpaid;     tenant = rent[0].tenants[0];      console.log("tenant",tenant._id)     console.log("rentamount",tenant.rentamount)     console.log("lastpayment", lastpayment)  }); 

Comments

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 -