在Mongoose中插入数组错误

时间:2017-11-22 22:43:49

标签: node.js mongodb mongoose

这是我的MongoDB架构,好像推送操作符不起作用

locationCoordinate : {
    type : [Number],
    index: '2d'
},

我从我的Node.js服务器的前端获取Post数据,该服务器具有经度和纬度

var event  = new Events({})
 (Events is the name of Schema )

我想推进这个数组, 所以这似乎不起作用

Error on this line 
       event.locationCoordinate.push(req.body.longitude);
       event.locationCoordinate.push(req.body.latitude);

然后通过

保存
event.save(function(err,result)){

}

结果可能是这样的 locationCoordinate [1,2]

1 个答案:

答案 0 :(得分:0)

var array = [];

array.push({"lng":req.body.longitude},{"lat":req.body.latitude}); // or
array.push(req.body.longitude,req.body.latitude); //just try mybe work
var Evnt = new Events()
               Evnt.type = array;
               Evnt.index = req.body.index;
Evnt.save(function(...){ ... });