Home > database >  mongodb nodejs asking a question regarding find method and nested objects in apis
mongodb nodejs asking a question regarding find method and nested objects in apis

Time:12-21

i have two sets of data in mongodb database both ahave one similar field as author_id so i have to check a conditon from one set and then pick the id from there and then see the id that matches in second set and print that but i am getting empty object

 const Pricesoff = async (req,res) =>{
     let abc = await BookModel.find( { price : { $gte: 50, $lte: 100} }) 
     //.select({author_id:1})
    
     let xyz = abc.author_id
     let obj = await authormodel.find({author_id: xyz})
     let newo = {
         "authorname":obj.author_name
     }
         res.send(newo)
}

CodePudding user response:

it might be as @user20042973 mentioned the .find() returns a cursor you might want to use findOne or add .lean() to find() check these urls they might help https://mongoosejs.com/docs/tutorials/lean.html#using-lean

the code is going to be something like this

 let abc = await BookModel.find( { price : { $gte: 50, $lte: 100} }).lean()
 //.select({author_id:1})

 let xyz = abc.author_id
 let obj = await authormodel.find({author_id: xyz}).lean()
 let newo = {
     "authorname":obj.author_name
 }
res.send(newo)
  • Related