Mongo DB Mongoose查询帮助

时间:2017-10-30 12:42:45

标签: node.js mongodb express mongoose

我此刻对此感到困惑。我尝试使用基于提交到某个路由(nodejs / expressjs)的数据的动态mongoose查询。请参阅下面的代码示例。

  reportingRouter.route('/filterData')

.post((req, res) => {
  console.log(req.body);


  function findFilteredResults(query) {
    // Performing a find on passdowns collection.
    // Criteria....
    // Find any passdown with a date that is greater than or equal to the startDate and less than or equal to the endDate.
    // Once found, if any, send back to client and render results in table rows.
    app.locals.passdowns.find(query, (err, result) => {
      if (err) {
        throw err;
      }
      // If the array of objects/entries empty, no results were found. Send 404.
      else if (result.length === 0) {
        console.log(result);          
        res.status(404);
      }
      // If the array has 1 object or more, we've found results and need to send 200 okay and result to client.
      else if (result.length >= 1) {
        console.log(result);
        res.status(200).send(result);
      }
    });
  }

  var mongooseQuery;

  if (req.body.shift !== '') {
    console.log('shift is not blank');
    findFilteredResults(`{ date_time: { $gte: new Date(req.body.startDate), $lte: new Date(req.body.endDate) }, shift: req.body.shift }`);      
  }
  else if (req.body.shift === '') {
    console.log('shift is blank');
    findFilteredResults(`{ date_time: { $gte: new Date(req.body.startDate), $lte: new Date(req.body.endDate) } }`);           
  }

});

我已将我的猫鼬发现逻辑放在一个函数中。基于发送到服务器的数据,可以针对不同的情况调用此方法。是否有可能做这样的事情,我将mongoose查询作为参数传递给函数?到目前为止,我所尝试的一切都失败了。

查询运行就好像它正在搜索" *"或者它没有找到任何东西,因为它认为以下是一个字符串。

{ date_time: { $gte: new Date(req.body.startDate), $lte: new Date(req.body.endDate) } }

0 个答案:

没有答案