简体   繁体   中英

Mongoose: geoNear and populate. Is it possible to populate fields while using executeDbCommand?

I'm doing a geospatial query using Mongoose and would like to also use populate().

Am I correct that populate() is a Mongoose specific command and won't work when using executeDbCommand? I've tried the following:

db.db.executeDbCommand({
    geoNear : "geopoints",
    near : [long, lat],
    populate : 'field', <-- doesn't work
    spherical : false, 
    distanceMultiplier:radPerMile,
    maxDistance : maxDis
}, function(err, result){
})

nor

db.db.executeDbCommand({
    geoNear : "geopoints",
    near : long, lat],
    spherical : false, 
    distanceMultiplier:radPerMile,
    maxDistance : maxDis
}, function(err, result){
}).populate('field', function(err, res){
   //also didn't work
})

any suggestions?

Here's how im doing it:

        var query = property.geoNear(
            [parseFloat(req.params.longitude), parseFloat(req.params.latitude)],
            {
              spherical : true,  
              distanceMultiplier: 3959,
              maxDistance : parseFloat(req.params.distance)/3959 
            }, function(err,docs) {
              if (err) throw err;

              //mapping each doc into new object and populating distance
              docs = docs.map(function(x) {
                var a = new property( x.obj );
                a.distance = x.dis;                 
                return a;
              });

              // populating user object
              property.populate( docs, { path: 'user', select: 'firstname lastname email' }, function(err,properties) {
                  if (err) res.json(err);
                  res.json(properties);
              });
        });

My solution is similar to "itaylorweb", but instead of mapping the docs i simply populate it.

geoNear result is object:

{
    dis": 0,
    "obj": { refField: someObjectId}
}

Notice the path of the populate is different, it has to be accessed via "obj". So:

var query = property.geoNear(
        [parseFloat(req.params.longitude), parseFloat(req.params.latitude)],
        {
          spherical : true,  
          distanceMultiplier: 3959,
          maxDistance : parseFloat(req.params.distance)/3959 
        }, function(err,docs) {
          if (err) throw err;

          // populating user object
          // nothice path!
          property.populate( docs, { path: 'obj.user', select: 'firstname lastname email' }, function(err,properties) {
              if (err) res.json(err);
              res.json(properties);
          });
    });

I'm do something similar to Shahaf H. and itaylorweb, but I'm not using callbacks. Rather, I'm using promises..Here's my solutoin:

```

let promise = Campground.geoNear(point, options);
promise.then(populateReview)
       .then((result)=>{ res.send(result);})
       .catch((error)=>{res.status(500).send(error);});

function populateReview(campgrounds) {
   return Campground.populate( campgrounds, {path: 'obj.Reviews', select: 'rating'});
}

```

Heres another way. I had to change to this approach to apply where clauses using query: {}

property.aggregate().near(
{ 
    near:[parseFloat(req.params.longitude), parseFloat(req.params.latitude)], 
    distanceField:"distance", 
    spherical:true, 
    distanceMultiplier:3959,
    maxDistance:parseFloat(req.params.distance)/3959,
    query: {
        monthlyFee : { 
                                $gte: parseInt(req.params.minPrice), 
                                $lte: parseInt(req.params.maxPrice)
                    },
        numberOfBedrooms : { $gte: parseInt(req.params.minBedrooms) }
    }
})
.exec(function(err,docs) {
    if (err) res.send(err);

    property.populate( docs, { path: 'user', select: 'firstname lastname email' }, function(err,properties) {
        if (err) res.json(err);
        res.json(properties);
    });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM