mongoose:geoNear并且填充。 在使用executeDbCommand时可以填充字段吗?

我正在做一个使用Mongoose的地理空间查询,并希望也使用populate()。

我正确的填充()是一个特定于Mongoose的命令,并且在使用executeDbCommand时不起作用吗? 我已经尝试了以下内容:

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

也不

 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 }) 

有什么build议么?

下面是如何做到这一点:

  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); }); }); 

我的解决scheme与“itaylorweb”类似,但不是映射文档,而只是填充它。

geoNear结果是对象:

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

注意填充的path是不同的,它必须通过“obj”来访问。 所以:

 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); }); }); 

我正在做一些类似于Shahaf H.和itaylorweb的东西,但我没有使用callback。 相反,我使用承诺..这是我的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'}); } 

“`

inheritance人另一种方式。 我不得不改变这种方法来应用where子句使用查询:{}

 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); }); });