“ MongoDB:未知的顶级运算符:$ expr”

时间:2018-10-13 11:35:36

标签: node.js mongodb mongoose

在React.js应用程序的Node.js后端中,我正在使用

"mongodb" (as db.version returns): "3.4.10",
"mongoose": "5.3.4"

我调查了一下问题,发现这可能是由于我使用的mongoDb版本。 所以我尝试将mongoDb切换到3.4.X,但仍然收到相同的错误消息:

"MongoError: unknown top level operator: $expr" 

因为这行代码:

match.$expr = {
            $lt: ["$distance", "$range"] // "calculated_distance < tutor_range"
         }

你知道为什么吗?这是完整的方法,如果您需要有关它的更多详细信息。

exports.search = (req, res) => {
  let lat1 = req.body.lat;
  let lon1 = req.body.lng;
  let page = req.body.page || 1;
  let perPage = req.body.perPage || 10;
  let radius = req.body.radius || 10000;

  let levelsIn = req.body.levels && req.body.levels.length !== 0 ? req.body.levels.map(level => {
    return ObjectID(level);
  }) : null;
  let subjectsIn = req.body.subjects && req.body.subjects.length !== 0 ? req.body.subjects.map(subject => {
    return ObjectID(subject);
  }) : null;

  var options = { page: page, limit: perPage,  sortBy: { updatedDate: -1 } }

  const isAdmin = req.user ? req.user.role === "admin" || req.user.role === "super-admin" : false;

  let match = {}

  if (levelsIn) match.levels = { $in: levelsIn };
  if (subjectsIn) match.subjects = { $in: subjectsIn }
  if (typeof req.body.activated !== "undefined") match.profileActivated = req.body.activated;
  if (req.body.from) match.createdAt = { $gte: new Date(req.body.from) };
  if (req.body.to) {
    if (match.createdAt) match.createdAt.$lte = new Date(req.body.to);
    else match.createdAt = { $lte: new Date(req.body.to) };
  }

  var aggregate = null;

  if (!isAdmin) {
    match.activated = true
    match.profileActivated = true
    match.profileOnline = true
  }

  if (lat1 && lon1) {

     match.$expr = {
        $lt: ["$distance", "$range"] // "calculated_distance < tutor_range"
     }

       aggregate = Tutor.aggregate([
     {

       "$geoNear": {
         "near": {
           "type": "Point",
           "coordinates": [lon1, lat1]
         },
         "distanceField": "distance", // this calculated distance will be compared in next section
         "distanceMultiplier": 0.001,
         "spherical": true
       }
     },
     {
       $match: match
     }
     ]);
  } else {
    aggregate = Tutor.aggregate([
      {
        $match: match
      }
    ]);
  }

  Tutor
    .aggregatePaginate(aggregate, options, function (err, result, pageCount, count) {
      if (err) {
        return res.status(400).send(err);
      }
      else {

        var opts = [
          { path: 'levels', select: 'name' },
          { path: 'subjects', select: 'name' },
          { path: 'assos', select: 'name' }
        ];
        Tutor
          .populate(result, opts)
          .then(result2 => {
            return res.send({
              page: page,
              perPage: perPage,
              pageCount: pageCount,
              documentCount: count,
              tutors: result2
            });
          })
          .catch(err => {
            return res.status(400).send(err);
          });
      }
    })
};

非常感谢您的回答和帮助!

0 个答案:

没有答案