how to insert particular array value in mongodb using angularjs

冷暖自知 提交于 2019-12-25 09:02:06

问题


I using angluarjs with mongodb.

I have schema is called array based schema.

schema structure :

var Schema = new Schema({
    UnitId: String,
    UnitName: String ,

    Details1:[{
      Details1ID:String,
        Name:String,
       Amount:Number 
   }],
   Details2:[{
        Details2ID:String,
        Name:String,
       Amount:Number 

   }],
  Details3:[{
      Details3ID:String,

   }],

I want query for inserting the req.body for only Details3.

how to do this?

NOTE:Req.body is Dynamic value


回答1:


first, you need to add { strict : false } options to your schema

var schema = new mongoose.Schema({
  // ..
}, {
  strict: false
})
module.exports = mongoose.model('MyModel', schema, 'myModel');

then create model with partial data

// Node.js server side
var model = new MyModel({
  // UnitId: "xxx",
  Details3: [...whatever]
});
model.save((error, data) => {
  console.log("model has been saved", data);
});


来源:https://stackoverflow.com/questions/43798329/how-to-insert-particular-array-value-in-mongodb-using-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!