php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64033 remove() doesn't return error when trying to delete a non-existant record
Submitted: 2013-01-21 03:25 UTC Modified: 2013-07-14 22:04 UTC
From: konstantin718 at gmail dot com Assigned:
Status: Not a bug Package: mongo (PECL)
PHP Version: Irrelevant OS: Ubuntu 12.04.1 LTS
Private report: No CVE-ID: None
 [2013-01-21 03:25 UTC] konstantin718 at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/mongocollection.remove#refsect1-mongocollection.remove-returnvalues
---

If you try to remove() a non-existent record from a collection with 'w' set to 1, remove() doesn't return an error or throw exception.

Instead it returns the array similar to this
Array ( [n] => 0 [connectionId] => 10726 [err] => [ok] => 1 ) 

So now we have to check if [n] is greater than 0 to actually know if remove() succeeded.

Test script:
---------------
$user_id = 'ffffffffffffffffffffffff'; // this doesn't exist in the collection.
$result = $collection->remove(array('_id' => new MongoID($user_id)), array('w' => 1));
print_r($result);



Expected result:
----------------
Array ( [n] => 0 [connectionId] => 10726 [err] => 'some error msg' [ok] => 0 ) 

Actual result:
--------------
Array ( [n] => 0 [connectionId] => 10726 [err] => [ok] => 1 ) 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-24 15:23 UTC] inefedor at gmail dot com
Why should it throw an exception? There weren't any errors. Your query just evaluated in zero changed records, but it's not an error like, say in MySQL or Postgres it's not an error when you do "DELETE FROM tbl WHERE id = 123456 /*where row with id=123456 is non-existent*/".
Actually, Mongo driver here just returns what Mongo itself sent to it.
 [2013-07-14 22:04 UTC] derick@php.net
-Status: Open +Status: Not a bug
 [2013-07-14 22:04 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Just like what inefedor said.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat May 25 15:01:27 2019 UTC