Mongoose aggregation with geonear

后端 未结 2 436
遥遥无期
遥遥无期 2021-01-07 10:32

I\'m trying to implement paging with Mongoose geoNear command. It seem skip is not supported with geoNear, and I understand aggregation would work (with paging with a perfor

相关标签:
2条回答
  • 2021-01-07 10:53

    You can use the aggregation framework for this and there is no real penalty as the operations are essentially the same.

    But while the mongoose .find() method presently has a problem with the $nearSphere operator which is equivalent, you can always grab the raw node driver connection object and do your query.

    You do not even need to throw away things like "population" if you are prepared to implement a little handling.

    Here is my test data:

    { 
        "_id" : "P1",
        "amenity" : "restaurant", 
        "shape" : { 
            "type" : "Point", 
            "coordinates" : [ 2, 2 ] 
        }
    }
    { 
        "_id" : "P3",
        "amenity" : "police",
        "shape" : { 
            "type" : "Point", 
            "coordinates" : [ 4, 2 ]
        }
    }
    { 
        "_id" : "P4",
        "amenity" : "police",
        "shape" : {
            "type" : "Point",
            "coordinates" : [ 4, 4 ]
        }
    }
    { 
        "_id" : "P2",
        "amenity" : "restaurant",
        "shape" : { 
            "type" : "Point",
            "coordinates" : [ 2, 4 ]
        }, 
        "info" : ObjectId("539b90543249ff8d18e863fb")
    }
    

    And the basic code to handle this:

    var mongoose = require('mongoose'),
        async = require('async'),
        Schema = mongoose.Schema;
    
    
    mongoose.connect('mongodb://localhost');
    
    var infoSchema = new Schema({
      "description": String
    });
    
    var shapeSchema = new Schema({
      "_id": String,
      "amenity": String,
      "shape": {
        "type": { "type": String },
        "coordinates": []
      },
      "info": { "type": Schema.Types.ObjectId, "ref": "Info" }
    });
    
    var Shape = mongoose.model( "Shape", shapeSchema );
    var Info = mongoose.model( "Info", infoSchema );
    
    
    Shape.collection.find(
      {
        "shape": {
          "$nearSphere": {
            "$geometry": {
              "type": "Point",
              "coordinates": [ 2, 4 ]
            }
          }
        }
      },
      {
        "skip": 0, "limit": 2
      },
      function(err,cursor) {
    
        cursor.toArray(function(err,shapes) {
    
          Shape.populate( shapes, { path: "info" }, function(err,docs) {
            if (err) throw err;
    
            console.log( JSON.stringify( docs, undefined, 4 ) );
          });
    
        });
    
      }
    );
    

    So there you got usage of both the skip and limit operations on the cursor, had a cursor returned and even processed the documents back into "Mongoose Documents" so you can call functions like .populate() on them.

    I would expect the current issue with $nearSphere to be fixed relatively soon though.

    Or using aggregate instead:

    Shape.aggregate(
      [
        { "$geoNear": {
            "near": {
              "type": "Point",
              "coordinates": [ 2, 4 ]
            },
            "spherical": true,
            "distanceField": "dis"
        }},
        { "$skip": 0 },
        { "$limit": 2 }
    
      ],
      function(err,shapes) {
        if (err) throw err;
        //console.log( shapes );
    
        shapes = shapes.map(function(x) {
          delete x.dis;
          return new Shape( x );
        });
    
        Shape.populate( shapes, { path: "info" }, function(err,docs) {
          if (err) throw err;
    
          console.log( JSON.stringify( docs, undefined, 4 ) );
        });
    
      }
    );
    

    Where you can do the same things such as use .populate(). Both cases return results like this with the "populated" field matched:

    {
        "_id": "P2",
        "amenity": "restaurant",
        "info": {
            "_id": "539b90543249ff8d18e863fb",
            "description": "Jamies Restaurant",
            "__v": 0
        },
        "shape": {
            "type": "Point",
            "coordinates": [
                2,
                4
            ]
        }
    },
    {
        "info": null,
        "_id": "P4",
        "amenity": "police",
        "shape": {
            "type": "Point",
            "coordinates": [
                4,
                4
            ]
        }
    }
    

    Of course if you do not need the spherical geometry calculation then the $near operator works perfectly fine with the Mongoose implementation of .find()

    0 讨论(0)
  • 2021-01-07 11:07
    location.aggregate({"$geoNear": { "near": { "type": "Point", "coordinates": [parseFloat(req.params.lon), parseFloat(req.params.lat)] }, "maxDistance":500, 'distanceField' : 'distance', spherical: true
          }}, 
        function(err, places) {
       if(!err){
               console.log(places);
                res.send('({"records":' + JSON.stringify(places) + '});');
            }
            else{
                console.log(err);
                res.send("error coming")
            }
        });`
    
    Above Query working fine in mongo shell but not working in mongoose 
    
    0 讨论(0)
提交回复
热议问题