2013-10-22 51 views
2

我正在做一个使用Mongoose的地理空间查询,并且还想使用populate()。猫鼬:geoNear并填充。在使用executeDbCommand时可以填充字段吗?

我正确的填充()是一个特定于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 
}) 

有什么建议?

回答

1

这里的IM如何做:

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

在我的测试,这是必要的'distance'字段添加到猫鼬这种方法的工作属性模型。否则,距离属性被丢弃。 –

+0

对不起,我忘了提及。 :) – itaylorweb

+0

感谢您回答一个老问题 - 希望尽快尝试您的解决方案。 – shi11i

0

继承人另一种方式。我不得不改变,以本办法适用,如果使用查询子句:{}

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

}); 
1

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

geoNear结果为对象:

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

通知的填入的路径是不同的,它具有通过“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); 
      }); 
    }); 
+0

这是一个更好的解决方案恕我直言,因为它不需要地图,它不会改变geoNear的答案结构。正是我需要的。谢谢。 – YoannM

1

我做一些类似Shahaf H.和itaylorweb的东西,但我不使用回调。相反,我使用promises..Here是我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'}); 
} 

```