mongodb unwated removal of an empty field on update

前端 未结 2 537
予麋鹿
予麋鹿 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 03:55

    Based off your problem, I would suggest re-trying "'created' => $this->getCreated() to the $query" provided your model has a created property defined, and your getter method is working, then this should use the current date on an insert, and the pre-set property value on update.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题