0

当我使用MongooseJS执行以下类型的查询时:

Place
  .find({location:{$geoWithin:{$box:[[0, -40],[151.0983703,-33.8674744]]}}})
  .exec(function(err, places) {
    ...
  });

我得到零个结果,并在err

[错误:不能将 $geoWithin 与 Array 一起使用。]

然而,如果我直接对 MongoDB 执行完全相同的标准,在命令行上:

> db.places.find({location:{$geoWithin:{$box:[[0, -40],[151.0983703,-33.8674744]]}}})

我得到了输出中显示的正确记录列表。

MongooseJS 代码我做错了什么?

4

1 回答 1

0

看起来$geoWithin在我使用的 Mongoose 版本中没有实现。

我暂时的解决方案是使用原生的 mongodb 库,直到 Mongoose 的稳定版本更新支持$geoWithin.

var mongodb = require('mongodb');
mongodb.MongoClient.connect(app.get('mongodb connection string'), function(err, db) {
    if (err) throw err;

    db.collection('places')
        .find({location:{
            $geoWithin:{
                $box:[[Number(req.query.northEastLng), Number(req.query.northEastLat)], 
                      [Number(req.query.southWestLng), Number(req.query.southWestLat)]]}}})
        .toArray(function(err, results) {
            if (!results)
                results = [];

            res.render('nearbyplaces', {
                results: results
            });

            db.close();
        });
});

这工作正常。

(注意:如果你真的想使用 Mongoose,你可以从他们的 Github 页面下载最新的不稳定版本。我只是不想这样做,因为它会使我的应用程序的部署复杂化。我希望能够下载所有内容npm install)。

于 2013-09-01T05:48:02.853 回答