社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  MongoDB

更新用户配置文件时,某些信息会从db、mongodb中删除。

Giorgio Martini • 5 年前 • 1526 次点击  

当我更新我的用户配置文件时,我的用户模型上位置内的字段邻近区域将被删除,这是我的用户模型,在这里您可以看到位置内的邻近区域:

const userSchema = new Schema({

  name: {
    type: String,
    required: 'Please supply a name',
    trim: true
  },
   location: {
    type: {
      type: String,
      default: 'Point'
    },
    coordinates: [{
      type: Number,
      required: 'You must supply coordinates!'
    }],
    address: {
      type: String,
      required: 'You must supply an address!'
    },
    vicinity: {
      type: String,
    },
  },
});

然后我有一个控制器来更新用户,在那里我有一个更新对象,并且附近不是三个,因为我不想更新它,我只想在寄存器上保存附近,而不是更新它:

exports.updateAccount = async (req, res) => {
  req.body.location.type = 'Point';

  const updates = {
    email: req.body.email,
    name: req.body.name,
    photo: req.body.photo,
    genres: req.body.genres,
    musicLinks: req.body.musicLinks,
    location: {
      type: req.body.location.type,
      coordinates: [
        req.body.location.coordinates[0],
        req.body.location.coordinates[1],
      ],
      address: req.body.location.address,
      // vicinity: req.body.location.vicinity,
    }
  };

  if(!updates.photo) delete updates.photo

  const user = await User.findOneAndUpdate(
    { _id: req.user._id },
    { $set: updates },
    { new: true, runValidators: true, context: 'query' }
  );
  req.flash('success', 'Updated the profile!');
  res.redirect('back');
};

但每次更新用户配置文件时,邻近字段都会被删除,同时请注意,在我为用户提交更新的表单中,邻近字段没有字段,因此它不会发送任何要更新的数据。

我猜是因为在控制器上我有一个更新对象,比如:

 location: {
      type: req.body.location.type,
      coordinates: [
        req.body.location.coordinates[0],
        req.body.location.coordinates[1],
      ],
      address: req.body.location.address,
      // vicinity: req.body.location.vicinity,
    }

以及它缺失的向量性。数据库试图保存整个location对象,因为它不在附近,所以会被删除。

如果是这样的话……我怎么跟mongo db说保留db上的值而不删除它?谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39013
 
1526 次点击  
文章 [ 2 ]  |  最新文章 5 年前