mongodb unwated removal of an empty field on update

前端 未结 2 540
予麋鹿
予麋鹿 2021-01-24 03:24

Currently i run a mongo query that looks similar to the following:

$query = array(
    \'user_id\' => $this->getUserId(),
    \'name\'    => $this->g         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-24 04:21

    This is because of your $query value. It is a common beginner mistake.

    Placing a whole query document into update like so:

    $query = array(
        'user_id' => $this->getUserId(),
        'name'    => $this->getName()
    );
    

    Without any operators will actually REPLACE the original document with this one.

    Instead to $set those fields to a new value you should use $set:

    $query = array('$set' => array(
        'user_id' => $this->getUserId(),
        'name'    => $this->getName() )
    );
    

    This should give you the effect you desire.

提交回复
热议问题