[英]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(). 我正在使用Mongoose进行地理空间查询,并且还想使用populate()。
Am I correct that populate() is a Mongoose specific command and won't work when using executeDbCommand? 我是否正确说populate()是猫鼬特定的命令,并且在使用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. 我的解决方案类似于“ itaylorweb”,但是我没有填充文档,而是直接填充它。
geoNear result is object: geoNear结果是对象:
{
dis": 0,
"obj": { refField: someObjectId}
}
Notice the path of the populate is different, it has to be accessed via "obj". 请注意,填充的路径不同,必须通过“ 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. 我正在做与Shahaf H.和itaylorweb类似的事情,但是我没有使用回调。 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: {}
我不得不更改为使用查询来应用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);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.