【问题标题】:How to update document data in nested object in MongoDB?如何更新 MongoDB 嵌套对象中的文档数据?
【发布时间】:2020-03-17 07:41:50
【问题描述】:

这是默认模式模型:-

const UserSettings = new Schema({
      ID: {  type: Number,  required: true, exists: false, unique : true },
      UserID : Number,
      Account : {
          Name : String,
          Email : {  type: String,  required: "email id is required", exists: false, unique : true },
          UserName : String,
          isFacebook :  {type : Boolean, default : false}
      },
      Notification : {
                  Trending : {
                      isEmail : {type : Boolean, default : true},
                      isPush :  {type : Boolean, default : true},
                      Button : {
                        isDaily :  {type : Boolean, default : false},
                        isWeekly :  {type : Boolean, default : false},
                        isOff : {type : Boolean, default : false}
                      }
                  },
                  Recommanded : {
                      isEmail : {type : Boolean, default : true},
                      isPush :  {type : Boolean, default : true},
                      Button : {
                        isDaily :  {type : Boolean, default : false},
                        isWeekly :  {type : Boolean, default : false},
                        isOff : {type : Boolean, default : false}
                      }
                  }
       }
});

当我保存值时,它将在架构中输入默认字段值,

    "Account" : {
    "Email" : "ted@gmail.com",
    "isFacebook" : true,
    "Name" : "ted"
},
"Notification" : {
    "Trending" : {
        "Button" : {
            "isDaily" : false,
            "isWeekly" : false,
            "isOff" : false
        },
        "isEmail" : true,
        "isPush" : true
    },
    "Recommanded" : {
        "Button" : {
            "isDaily" : false,
            "isWeekly" : false,
            "isOff" : false
        },
        "isEmail" : true,
        "isPush" : true
    }
}

在更新的情况下,我必须传递选定的参数,比如只需要更新

Account{
Email : "ted1@gmail.com"
},
Notification {
  Trending {
      isPush : false,
  }    
}

那么我将如何使用 mongo 更新查询?

我已经尝试过这个解决方案来更新价值,但它会删除现有的价值:-

  return UserSettings.updateOne(
       {$and: [{  UserID: args.UserID },{ Status : 1 }]},
        { $push : { "Account":  args.Account  }},
       {
         upsert: true,
         returnNewDocument: true
       },
    );

还有一个:-

return UserSettings.updateOne(
       {$and: [{  UserID: args.UserID },{ Status : 1 }]},
       args,
       {
         upsert: true,
         returnNewDocument: true
       },
    );

【问题讨论】:

  • 请从模型文件上传您的代码
  • 您想在 Account 对象中使用唯一的电子邮件?
  • 是的,@Mr.Gandhi 电子邮件验证是 graphql 的默认处理,我有嵌套数据更新问题

标签: node.js mongodb mongoose graphql


【解决方案1】:

试试这个:

let Account = args.Account
let condition = { $and: [ {  UserID: args.UserID },{ Status : 1 }]}, { 'Account.Email': args.Account.Email }] }; // here check email in sub document
UserSettings.updateOne(condition, { $set: { Account: Account } }).exec((err, data) => {
    if (err || data.nModified == 0) {
        UserSettings.updateOne({ UserID: args.UserID  }, { $push: { Account: Account } }).exec((err, data) => {
             return {'message':'Updated successfully','data':data}; // Send proper response as per your need
        });
    }
    else {
             return {'message':'Failed'};// Send proper response as per your need
    }
});

【讨论】:

  • 现有值被删除,如果字段存在并且如果字段不在数组中,我想更新旧值然后需要推送
  • err { MongoError: The field 'Account' must be an array but is of type object in document {_id: ObjectId('5dd52ff4701b262558ef9469')} 这个错误返回
  • 只能对数组进行推送操作。因此,如果要进行推送操作,您将需要更改模型
  • 现在它是模型中的对象
猜你喜欢
  • 2021-02-13
  • 1970-01-01
  • 2012-05-18
  • 2017-06-05
  • 2016-08-04
  • 2010-11-11
  • 1970-01-01
  • 2021-01-23
  • 2017-04-28
相关资源
最近更新 更多