I am using the node-geoip module and executing an aggregation request. The schema with which I execute the query is as follows:
var mongoose = require('mongoose'); require('./location.js'); module.exports = mongoose.model('Region',{ attr1: Number, attr2: String, attr3: String, locations:[mongoose.model('Location').schema] });
and
var mongoose = require('mongoose'); module.exports = mongoose.model('Location',{ attr1: Number, latlong: { type: [Number], index: '2d' }, });
I need to perform the $ geoNear operation in an aggregation request, but I ran into a few problems. Firstly, here is my aggregation method:
var region = require('../models/region'); var geo = geoip.lookup(req.ip); region.aggregate([ {$unwind: "$locations"}, {$project: { attr1 : 1, attr2 : 1, locations : 1, lower : {"$cond" : [{$lt: [ '$locations.attr1', '$attr1']}, 1, 0]} }}, { $geoNear: { near: { type:"Point", '$locations.latlong': geo.ll }, maxDistance: 40000, distanceField: "dist.calculated" } }, { $sort: { 'locations.attr1': -1 } }, {$match : {lower : 1}}, { $limit: 1 } ], function(err,f){...});
The first problem I get is that obviously geoNear should be in the first pipeline stage: exception: $geoNear is only allowed as the first pipeline stage . So my question is: can I search for geoNear in subdocuments without untying them? If so, how?
Another error message I get is errmsg: \"exception: 'near' field must be point\" . What does this mean and what does it mean for my code? I tried using near as:
near: { type:"Point", '$locations.latlong': geo.ll },
javascript mongodb mongoose aggregation-framework geolocation
Jordan
source share