如何将JSON数组保存到mongodb集合中

我试图将下面的数组保存到我的文档中的mongodb

{"Category_Id":"54c9c206e1f512456ada778b","Sub_category_Id":"54c9c24ee1f512456ada778d", "features":[{"specification_field_type":"textbox","specification_name":"color","_id":"551e5f2e3bbe4691142bdeba","__v":0,"$$hashKey":"object:95"}, {"specification_field_type":"textbox","specification_name":"Call Features","_id":"551e616d3bbe4691142bdebf","__v":0,"$$hashKey":"object:97"}]} 

这里是我的node.js代码保存mongodb中的后期值

 var Category_Id = req.body.Category_Id; var Sub_category_Id = req.body.Sub_category_Id; var features = req.body.features; var property =new db.properties(); property.Category_Id = Category_Id; property.Sub_category_Id = Sub_category_Id; property.features = features; property.save(function (err){ var message = JSON.parse('{"status":"success","message":"Features added"}'); return res.send(200, message); } 

尽pipe查询被执行,但是特征信息以这种方式保存 在这里输入图像说明

集合模式是

 var properties = new Schema({ Category_Id : { type: String}, Sub_category_Id : { type: String}, features : { type: String} }); 

有人可以帮我解决这个问题吗? 谢谢。

看来你声明的features是在你的模式中的一个string,而不是一个子文档数组。 因此,在保存之前,Mongoose将req.body的数组req.body为一个string。 以下调整到您的架构应该让你在正确的轨道上:

 var properties = new Schema({ Category_Id : { type: String}, Sub_category_Id : { type: String}, features : [{ specification_field_type": String, specification_name: String // any other paths for features }] });