问题
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