如何使用nodejs更新mongoose中的数组对象键值数组

时间:2018-03-02 09:40:06

标签: javascript node.js mongodb express mongoose

如何更新est_price值的Options数组,请你有建议。谢谢很多,我想知道是否有人给出任何解决方案? 我试过了,

ProductDal.update(
   {name: req.doc.name, parameters: { $elemMatch: { param_name: body.parameter} },'parameters.options': { $elemMatch: { opt_name: body.opt_name} } },
{ $set: { 'parameters.0.options.$.opt_name': 'Test', } 
});

代码

const mongoose  = require('mongoose');
const moment    = require('moment');
const paginator = require('mongoose-paginate');
var Schema = mongoose.Schema;


var ProductSchema = new Schema({
  name:  { type: String },
  sector: { type: Schema.Types.ObjectId, ref: 'Sector'},
  service: { type: Schema.Types.ObjectId, ref: 'Service'},
  parameters:[
     {//  _id:false,

            param_name: { type: String},
             options:[

                    { 

                        opt_name: { type:String },
                        est_price:{ type:String}
                    }
            ]
        }
  ],
  created_by:    { type: Schema.Types.ObjectId, ref: 'User'},
  archived :     { type: Boolean, default:false },
  archived_at:   { type: Date, default:null},
  created_at:    { type: Date, default:new Date()},
  updated_at:    { type: Date, default:null},

},{versionKey: false});
// add middleware to support pagination
ProductSchema.plugin(paginator);
// Expose the Product Model
module.exports = mongoose.model('Product',ProductSchema);

如何更新est_price值的Options数组,请您提出建议。谢谢

0 个答案:

没有答案