2017-08-20 192 views
0

我是MongoDB的新手,我想抓住所有適合createdBy或to的「帖子」。查詢mongoDB查找()

router.get('/getPublicProfilePosts/:username',(req,res)=>{ 
    if(!req.params.username){ 
     res.json({success:false,message:"No username provided"}); 
    } 
    else{ 
     User.findOne({username:req.params.username},(err,user)=>{ 
      if(err){ 
       res.json({success:false,message:'Something went wrong '+err}); 
      } 
      else{ 
       if (!user) { 
        res.json({success:false,message:'User not found'}); 
       } 
       else{ 
        Post.find({createdBy:user.username, to:user.username},(err,posts)=>{ //this one 
         if(err){ 
          res.json({success:false,message:'Something went wrong '+err}); 
         } 
         else{ 
          if (!posts) { 
           res.json({success:false,message:'Posts not found !'}); 
          } 
          else{ 
           res.json({success:true,posts:posts}); 
          } 
         } 
        }) 
       } 
      } 
     }); 
    } 
}); 

Post.find({createdBy:user.username,於:user.username}。所以我想聲明必須尊重BOTH createdBy,並返回一個空數組

回答

0

我只是找到了答案

Post.find({$or : [ 
         { createdBy:user.username}, 
         { to: user.username } 
        ]},