mongoose查询结果缺少字段

时间:2016-09-26 19:47:24

标签: node.js mongodb express mongoose

我刚开始使用node express框架和mongo作为数据库。

我创建了这样的架构:

var JsonSchema = new Schema({                                                               
 type: String,                                                                           
 properties: {                                                                           
     OBJECTID: Number,                                                                   
     AREA: Number,                                                                       
     PERIMETER: Number,                                                                  
     ESYE_CODE: Number,                                                                  
     Descriptio: String                                                                  
 },                                                                                      
 geometry: {                                                                             
     type: String,                                                                       
     coordinates: [Number, Number]                                                       
 }                                                                                       
});

然后我查询:

router.get('/mapjson/:OBJECTID', function(req, res) {
 if(req.params.OBJECTID) {
     Json.findOne({OBJECTID: req.params.OBJECTID }, {}, function(err, docs){
         res.json(docs);
 } else {
     console.log("THERE WAS AN ERROR HERE!!!");
 }
});

但我得到的结果是缺少geometry字段。

我得到的结果样本:

{ 
"_id":"57e43ec60534d33ccc13099b",
"type":"Feature",
"properties":{
    "OBJECTID":212428,
    "AREA":131.001991421,
    "PERIMETER":49.9141344212,
    "ESYE_CODE":147,
    "Descriptio":"Ελληνικά"
    }
 }

以及从mongo shell查询集合时得到的内容:

db.points.findOne({'properties.OBJECTID': 212428})
{
    "_id" : ObjectId("57e43ec60534d33ccc13099b"),
    "type" : "Feature",
    "properties" : {
            "OBJECTID" : 212428,
            "AREA" : 131.001991421,
            "PERIMETER" : 49.9141344212,
            "ESYE_CODE" : 147,
            "Descriptio" : "Ελληνικά"
    },
    "geometry" : {
            "type" : "Point",
            "coordinates" : [
                    23.812561006040106,
                    38.093951650544334
            ]
    }
}

1 个答案:

答案 0 :(得分:1)

在坐标中,您可以将类型设置为数组:

 geometry: {                                                                             
     type: String,                                                                       
     coordinates: Array                                                       
 }