2016-08-05 93 views
0

我有一个模式,看起来像这样:猫鼬之内时,查询或查询

var UserSchema = new Schema({ 
    name: {type : String, required: true}, 
    location_a: {type: [Number], default: [0,0], index: '2dsphere'}, 
    location_b: {type: [Number], default: [0,0], index: '2dsphere'} 
}); 

我搜索的输入有一个位置和距离。现在我想让位于location_a或location_b附近的用户接近搜索到的位置。

我已经尝试了查询,看起来像这样:

query = query.and([ 
     { $or: [ 
      query.where('location_a').near({ 
       center: {type: 'Point', coordinates: [long, lat]}, 
       maxDistance: distance * 1000, spherical: true 
      }), 
      query.where('location_b').near({ 
       center: {type: 'Point', coordinates: [long, lat]}, 
       maxDistance: 100 * 1000, spherical: true 
      }) 
     ]} 
    ]); 

但它使我的应用程序崩溃与“的RangeError:最大调用堆栈大小超出”

+0

你的''和'没有做任何事情,而你的'$或'周围的'[]'括号在不正确的位置 –

+0

你确定[]是问题吗?我应该在哪里放置它们? – Vetm

+0

请在显示查询前指定查询的方式 –

回答

0

我使用了错误的格式,在我查询。这将是你如何做到这一点。

query = query.and([ 
     { $or: [ 
      { 
       "at_student_location": { 
        "$near": [12.5386, 41.884], 
        "$maxDistance": 1000 
       } 
      } 
      , 
      { 
       "at_home_location": { 
        "$near": [12.5386, 41.884], 
        "$maxDistance": 1000 
       } 
      } 
     ]} 
    ]); 

的问题是,一个蒙戈只接受每个查询一个geoNear表达式所以这是不可能做到这一点。