简体   繁体   中英

PHP MongoDB $exist not working

$collection->update(array("_id"=>new MongoId($uid), "phonenumber"=> $exist => array(FALSE),$set("phone"=>"1223444"));

I would like to know why my $exist query is not working with PHP, and Mongodb if anyone could point me in the right direction that would be helpful.

Ok in the collection database there is no row called phonenumber and if there is no phonenumber i want it to insert one, but if there is phonenumber dont do anything.

You have a couple of syntax issues.

  1. You're missing arrays at the second level
  2. Your operators need single quotes around them ( $exist )

Here is a cleaned up sample:

$collection->update(
    array( "_id"=> new MongoId($uid), 
           array("phonenumber"=> array('$exists' => false))
         ),
    array( '$set' => array("phone"=>"1223444") )
);

This should be :

$collection->update(
    array( 
        "_id"         => new MongoId($uid), 
        "phonenumber" => array('$exists' => false)
    ),
    array( '$set' => array("phone"=>"1223444") )
);

There was an array level too much in the response Gates VP

Try to do this not equal operator

$collection->update(
    array( "_id"=> new MongoId($uid)),
    array( '$set' => array("phone"=>"1223444") )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM