MongoDB /Mongoose - Агрегация с geoNear и вложенными документами

I'm using the node-geoip module and performing an aggregation query. The schema against which I am performing the query looks like this:

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 a $geoNear operation in an aggregation query, but I'm running into a few problems. First, 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'm getting is that apparently geoNear must be in the first stage of the pipeline: exception: $geoNear is only allowed as the first pipeline stage. So my question is, can I perform a geoNear search in the subdocuments without unwinding them? If so, how?

The other error message I get is errmsg: \"exception: 'near' field must be point\". What does this mean and what does it imply for my code? I have tried using near as:

near: { type:"Point", '$locations.latlong': geo.ll },
7 голосов | спросил jordan 25 MarpmWed, 25 Mar 2015 19:22:07 +03002015-03-25T19:22:07+03:0007 2015, 19:22:07

0 ответов


Похожие вопросы

Популярные теги

security × 330linux × 316macos × 2827 × 268performance × 244command-line × 241sql-server × 235joomla-3.x × 222java × 189c++ × 186windows × 180cisco × 168bash × 158c# × 142gmail × 139arduino-uno × 139javascript × 134ssh × 133seo × 132mysql × 132