mongoose查询不返回该位置

时间:2015-08-11 05:48:17

标签: node.js mongodb mongoose

这是我的文档中的一条记录,其中包含我需要获取的位置字段。

{
    "_id": {
        "$oid": "559c152fa439a961c357f931"
    },
    "POST_ID": "354-20160",
    "MS_ID": "-",
    "MS_SPACEID": 0,
    "CAP_COLOR": "Grey",
    "METER_TYPE": "SS",
    "SMART_METE": "Y",
    "ACTIVESENS": "N",
    "JURISDICTI": "SFMTA",
    "ON_OFF_STR": "ON",
    "OSP_ID": 0,
    "STREET_NUM": 2016,
    "STREETNAME": "CHESTNUT ST",
    "STREET_SEG": 3977000,
    "RATEAREA": "Area 5",
    "SFPARKAREA": "Marina",
    "LOCATION": {
        "type": "Point",
        "coordinates": [
            37.8007983983,
            -122.4368696024
        ]
    }
}

这就是我在mongoose中定义架构的方式

// mongoose scehma
var mongoose = require('mongoose');
var parkingSchema = mongoose.Schema({
    "POST_ID": String,
    "MS_ID": String,
    "MS_SPACEID": Number,
    "CAP_COLOR": String,
    "METER_TYPE": String,
    "SMART_METE": String,
    "ACTIVESENS": String,
    "JURISDICTI": String,
    "ON_OFF_STR": String,
    "OSP_ID": Number,
    "STREET_NUM": Number,
    "STREETNAME": String,
    "STREET_SEG": Number,
    "RATEAREA": String,
    "SFPARKAREA": String,
    "LOCATION": {
        "type": String,
        "coordinates": [Number]
    }
});

var Parking = mongoose.model('parking_info', parkingSchema);
module.exports = Parking;

我的返回记录的查询:

app.get("/parkings", function(req, res){
    Parking.find(function(err, parkings){
        if(err){
            console.log('error..');
            return handleError(err);
        }
        else {
            console.log('returning parkings..');
            res.send( parkings);
        }

    });

});

给出:

    [
{"_id":"559c152fa439a961c357f931","POST_ID":"354-20160","MS_ID":"-","MS_SPACEID":0,"CAP_COLOR":"Grey","METER_TYPE":"SS","SMART_METE":"Y","ACTIVESENS":"N","JURISDICTI":"SFMTA","ON_OFF_STR":"ON","OSP_ID":0,"STREET_NUM":2016,"STREETNAME":"CHESTNUT ST","STREET_SEG":3977000,"RATEAREA":"Area 5","SFPARKAREA":"Marina"},
{"_id":"559c1530a439a961c357f932","POST_ID":"354-21030","MS_ID":"-","MS_SPACEID":0,"CAP_COLOR":"Green","METER_TYPE":"SS","SMART_METE":"Y","ACTIVESENS":"N","JURISDICTI":"SFMTA","ON_OFF_STR":"ON","OSP_ID":0,"STREET_NUM":2103,"STREETNAME":"CHESTNUT ST","STREET_SEG":3979000,"RATEAREA":"Area 5","SFPARKAREA":"Marina"},
{"_id":"559c1530a439a961c357f933","POST_ID":"354-21160","MS_ID":"-","MS_SPACEID":0,"CAP_COLOR":"Yellow","METER_TYPE":"SS","SMART_METE":"Y","ACTIVESENS":"N","JURISDICTI":"SFMTA","ON_OFF_STR":"ON","OSP_ID":0,"STREET_NUM":2116,"STREETNAME":"CHESTNUT ST","STREET_SEG":3979000,"RATEAREA":"Area 5","SFPARKAREA":"Marina"},
{"_id":"559c1530a439a961c357f934","POST_ID":"363-05250","MS_ID":"-","MS_SPACEID":0,"CAP_COLOR":"Grey","METER_TYPE":"SS","SMART_METE":"N","ACTIVESENS":"N","JURISDICTI":"SFMTA","ON_OFF_STR":"ON","OSP_ID":0,"STREET_NUM":525,"STREETNAME":"COLUMBUS AVE","STREET_SEG":4295000,"RATEAREA":"Area 3","SFPARKAREA":""}
...

为什么位置不在查询结果中?有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

问题在于LOCATION字段的声明:

var parkingSchema = mongoose.Schema({
    ...
    "LOCATION": {
        "type": String,           //Mongoose assumes the field is of String type.
        "coordinates": [Number]
    }
});

您可以通过执行以下操作来解决此问题:

var parkingSchema = mongoose.Schema({
    ...
    "LOCATION": {
        "type": {"type": String},           
        "coordinates": [Number]
    }
});