Click here to Skip to main content
15,886,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to delete a record from mongodb database using php by passing mongodb ID. The code executes fine but it's not deleting the specified document, does anyone see any problem with the code below ??

C#
 $postData = json_decode(file_get_contents("php://input"), true);

    $conn = new MongoClient();
    $db = $conn->selectDB('employees');
    $collection = new MongoCollection($db, 'employees');
    $emp = array('id' => new MongoID($postData['$id']), true);

    try {
        $collection->remove($emp, array('justOne' => true));
    }
    catch (MongoCursorException $e) {
        die(json_encode(array('message' => $e->getMessage())));
    }

    echo ('The employee has been deleted successfully');
}


What I have tried:

I have tried Stackoverflow but people there don't seem to take interest in this question.
Posted
Comments
Richard MacCutchan 31-May-16 9:01am    
You do not check the result status of your call to the remove method. Do not display meesages like "The employee has been deleted successfully" when you have not checked that it actually worked.

To find out why it failed, use your debugger to check the values of your input variables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900