$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.
In MongoDB, we can check the existence of the field in the specified collection using the $exists operator. When the value of $exists operator is set to true, then this operator matches the document that contains the specified field(including the documents where the value of that field is null).
You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});
To add field or fields to embedded documents (including documents in arrays) use the dot notation. See example. To add an element to an existing array field with $addFields , use with $concatArrays . See example.
The { item : null } query matches documents that either contain the item field whose value is null or that do not contain the item field. The query returns both documents in the collection.
You have a couple of syntax issues.
$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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With