嵌套文档上的2dsphere索引无法正常工作

时间:2014-12-22 11:16:49

标签: mongodb mongoose geospatial

我想在以下架构中保存多个位置并运行地理位置查询。

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

/**
 * Offer Schema
 */
 var LocSchema = new Schema({
  'type': {
      type: String,
      required: true,
      enum: ['Point', 'LineString', 'Polygon'],
      default: 'Point'
    },
    coordinates: [Number]
 });
var LocationSchema = new Schema({
    address:{type:String},
    loc:[LocSchema]
}); 
var OfferSchema = new Schema({
    name: {
        type: String,
        default: '',
        required: 'Please fill Offer name',
        trim: true
    },
    description:
    {
        type:String,
        default:'',
        required:'Please fill offer description',
        trim: true
    },
    shortDesc:
    {
        type:String,
        default:'',
        trim: true
    },
    image:
    {
        type:String,
        default:'',
        required:'Please upload image'
    },
    locations:[LocationSchema],
    created: {
        type: Date,
        default: Date.now
    },
    startDate: {
        type: Date,
        default: Date.now
    },
    endDate: {
        type: Date,
        default: Date.now
    },
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    },
    category:
    {
     type: Schema.ObjectId,
      ref :'Category'
    }

});
OfferSchema.index({'locations.loc': '2dsphere'});
mongoose.model('Offer', OfferSchema);

当我运行保存对象时,我得到了"出了点问题"错误,但是如果删除索引,我可以保存文档。 有人可以告诉我,如果我做错了什么。

0 个答案:

没有答案