Mongodb Update不适用于嵌套对象数组

时间:2018-05-07 12:24:12

标签: node.js mongodb express

我想在嵌套对象中推送元素。我没有收到任何错误,但记录没有更新。

{ 
    "_id" : ObjectId("5aa589b31b79fbe3312d3a1b"), 
    "settings" : {
        "clientSetting" : [
            "Hello1",
             "Hello2",
        ], 
        "adminSetting" : [
            "Hello1",
             "Hello2",
        ], 
    }
}

模态声明: -

import mongoose from 'mongoose';

const { Schema } = mongoose;

const settingSchema = new Schema(
  {
    settings: {
      clientSetting: [
        { type: String },
      ],
      adminSetting: [
        { type: String },
      ],
    },
  },
  { collection: 'mySettings' },
);

export default mongoose.model('MySetting', settingSchema, 'mySettings');

这是我累的

export const update = (req, res) => {

    MySetting.find((err, globalSettings) => {
     MySetting.update(
         { _id: mongoose.Types.ObjectId(globalSettings[0]._id) },
         { $push: { 'settings.$.clientSetting': { type: req.body.value } } },);
      });
}

这里我想将{type:req.body.value}推送到clientSetting。

0 个答案:

没有答案