doubiao7410 2014-04-16 19:26
浏览 54
已采纳

如何更新用户信息? (如果用户编辑了他的信息)

Let's assume we are creating a site where people can register with some information (first name, last name, email address, etc.). There must be a way for users to change their information. So user can edit first name field or last name field or both. Please help to find a general and good way for this. I mean if we going to check which field was updated and for each possible case give specific document to mongo's update function is not a good idea, is it?

$employee->update(
    array( '_id' => new MongoId('4ba667b0a90578631c9caea1')),
    array( '$set' => array( 'salary' => '1000' ) )
);

This code will only update the user's salary. But there are too many information. What if user's email or phone number changed? Please help to find a general way.

  • 写回答

1条回答 默认 最新

  • duanlang1196 2014-04-16 20:02
    关注

    I would validate my data before building the MongoDB document, then build an array with the validated data, and use the array to build the MongoDB document.

    $dataArray = array();
    $dataArray['salary'] = '1000';
    $dataArray['other_key'] = 'other_val';
    
    $employee->update(
    array( '_id' => new MongoId('4ba667b0a90578631c9caea1')),
    array( '$set' => $dataArray )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大