使用Mongoose进行geoJSON支持的搜索

时间:2015-10-21 09:38:41

标签: mongodb mongoose mongodb-query

我试图让Mongoose计划执行基于点的$near查找。我只是试图获取随机文档,并且我从this answer.

获取指南

我试过了:

    Video.where('randomTag').near({
        center: {
            type: 'Point',
            coordinates: [-98.18, 19]
        }
    }).exec(function (err, videos) {
        console.log(err)
        response.json(videos);
    })

此外:

    Video.near('randomTag',{
        center: {
            type: 'Point',
            coordinates: [-98.18, 19]
        }
    }).exec(function (err, videos) {
        console.log(err)
        response.json(videos);
    })

    Video.find({
        randomTag: {
            $near: {
                $geometry: {
                    type: "Point",
                    coordinates: [Math.random()/Math.random(), 0]
                }
            }
        }
    }).exec(function (err,videos) {
        response.json(videos)
    })

对于所有这些尝试,我收到了这个错误:

error: Can't use $near with Number.

我已经获得了所需的索引:

{randomTag: '2dsphere'} 

架构如下:

{
  videoId: String,
  randomTab: Array(Number),
  title: String,
  playCount: Number
}

以下是一些示例数据。

{
    "videoId": "aRDAz55d-y",
    "randomTag": [2.255285185646381,0],
    "title": "La décima, inolvidable",
    "playCount": 254111
}

{
    "videoId": "vAFj32af",
    "randomTag": [0.4515513067517708,0],
    "title": "SILePetitPrince",
    "playCount": 900
}

这是完整的错误跟踪:

Error: Can't use $near with Number.
    at SchemaNumber.castForQuery (/storage/home/dev/final-cut/node_modules/mongoose/lib/schema/number.js:261:13)
    at module.exports (/storage/home/dev/final-cut/node_modules/mongoose/lib/cast.js:196:39)
    at Query.cast (/storage/home/dev/final-cut/node_modules/mongoose/lib/query.js:2341:10)
    at Query.find (/storage/home/dev/final-cut/node_modules/mongoose/lib/query.js:998:10)
    at Function.find (/storage/home/dev/final-cut/node_modules/mongoose/lib/model.js:1026:13)
    at sayHello (/storage/home/dev/final-cut/api/controllers/api.js:23:15)
    at Layer.handle [as handle_request] (/storage/home/dev/final-cut/node_modules/express/lib/router/layer.js:95:5)
    at next (/storage/home/dev/final-cut/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/storage/home/dev/final-cut/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/storage/home/dev/final-cut/node_modules/express/lib/router/layer.js:95:5)
    at /storage/home/dev/final-cut/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/storage/home/dev/final-cut/node_modules/express/lib/router/index.js:330:12)
    at next (/storage/home/dev/final-cut/node_modules/express/lib/router/index.js:271:10)
    at Function.handle (/storage/home/dev/final-cut/node_modules/express/lib/router/index.js:176:3)
    at router (/storage/home/dev/final-cut/node_modules/express/lib/router/index.js:46:12)
    at Layer.handle [as handle_request] (/storage/home/dev/final-cut/node_modules/express/lib/router/layer.js:95:5)
GET /api/hello 500 20.560 ms - -

Math.random()使用的原因是因为随机需要。我有什么遗失的吗?

1 个答案:

答案 0 :(得分:1)

对我来说很好看。你必须做一些不同于这个清单的事情:

var async = require('async'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var videoSchema = new Schema({
  videoId: String,
  randomTag: [Number],
  title: String,
  playCount: Number
});

videoSchema.index({ "randomTag": "2dsphere" });

var Video = mongoose.model( 'Video', videoSchema );

mongoose.connect('mongodb://localhost/test');

mongoose.set("debug",true);

Video.find(
  {
    "randomTag": {
      "$near": {
        "$geometry": {
          "type": "Point",
          "coordinates": [Math.random()/Math.random(),0]
        }
      }
    }
  },
  function(err,videos) {
    if (err) throw err;
    console.log(videos);
    mongoose.disconnect();
  }
);

这给了我这样的结果:

Mongoose: videos.ensureIndex({ randomTag: '2dsphere' }) { background: true }
Mongoose: videos.find({ randomTag: { '$near': { '$geometry': { type: 'Point', coordinates: [ 1.8434117849022023, '\u001b[33m0\u001b[39m' ] } } } }) { fields: undefined }
[ { playCount: 254111,
    title: 'La décima, inolvidable',
    randomTag: [ 2.255285185646381, 0 ],
    videoId: 'aRDAz55d-y',
    _id: 5627616d76dfa5adcd39fd38 },
  { playCount: 900,
    title: 'SILePetitPrince',
    randomTag: [ 0.4515513067517708, 0 ],
    videoId: 'vAFj32af',
    _id: 5627616d76dfa5adcd39fd39 } ]