MongoDB:将聚合与$ geoNear结合使用不会返回所有结果-Node.js

时间:2018-10-08 10:06:00

标签: node.js mongodb reactjs aggregate geonear

使用geoNear时,aggregate不会返回它应返回的所有结果。我已经仔细检查过,并且有一些结果在范围内并且与查询相对应,但是没有返回。只有18个被退回,我应该得到30个...

要检索的文档是具有一定范围(整数值)的导师,该范围对应于他们将移动多远。举例来说,如果客户在巴黎48街de Varenne找一位导师,我们应该只检索可以前往巴黎48 rue de Varenne的导师(搜索位置与导师位置小于导师范围

这是查询:

aggregate = Tutor.aggregate([
  {
    "$geoNear":
    {
      "near":
      {
        "type": "Point",
        "coordinates": [lon1, lat1]
      },
      "distanceField": "distance",
      "spherical": true,
      "maxDistance": radius
    }
  },
  {
    $match: match
  }
]);

您知道我为什么收到这个问题吗?我在某处阅读过,可能是文档太大的问题?但是,我不知道该如何检查。

非常感谢您的帮助!

PS: 如果您想查看完整的方法:

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) {

    aggregate = Tutor.aggregate([
      {
        "$geoNear":
        {
          "near":
          {
            "type": "Point",
            "coordinates": [lon1, lat1]
          },
          "distanceField": "distance",
          "spherical": true,
          "maxDistance": radius
        }
      },
      {
        $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);
          });
      }
    })
};

还有我要查询的模型的简化版本:

import mongoose from 'mongoose';
import validate from 'mongoose-validator';
import { User } from './user';
import mongooseAggregatePaginate from 'mongoose-aggregate-paginate';

var ObjectId = mongoose.Schema.Types.ObjectId;


var tutorSchema = mongoose.Schema({
    location: {
        address_components: [
            {
                long_name: String,
                short_name: String,
                types: String
            }
        ],
        description: String,
        lat: Number,
        lng: Number

    },
    range: {
        type: Number,
        default: 15
    },
    loc: {
        type: { type: String },
        coordinates: []
    }
});

tutorSchema.plugin(mongooseAggregatePaginate);
tutorSchema.index({ "loc": "2dsphere" });
module.exports = {
    Tutor
};

0 个答案:

没有答案