Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

[MongoDB] Deleting multiple entries based on ids

Ok, so i'm trying to delete multiple entries of a collection (carriers) based on their id which is passed to controller via GET method.

Basically what I do is get the ids and send them to a popup (view) that have a button which makes an ajax call to the action that deletes the entries based on ids (they're passed to this action via POST method).

after that, I use this action to delete them (which is not working):

public function removeCarrierAction()
    {
        $request = new Phalcon\Http\Request();
        if($request->isPost() == true){
            if($request->isAjax() == true){

                //Get the ids that came into a string like "id1,id2,id3,..idn"
                $ids = explode(",",$request->getPost(0));
                //I've already tried with Carriers::findById($ids), but it didin't work either
                $carriers = Carriers::find(["_id" => $ids]);

                //delete all returned carriers
                foreach($carriers as $carrier){
                    if($carrier->delete() == true){
                        echo print_r($ids);
                    }else{
                        echo "error";
                    }
                }
            }
        }
    }

It for some reason delete all the entries. Not only the based on the ids i've passed. Any idea of how to make it works propely?



2.8k

Sound advice from @xAockd. Also take a look at the arguments of the find() method. It seems you are missing a nested array.



38.7k
Accepted
answer

It has been some time since this problem showed up. I found the solution and forgot to post it here, but here it is, if anyone is still interested:

Basically the problem was that the array of ids wasn't converted to MongoId() objects, so Mongo didn't knew what do with them (they were reconized as mere strings). The solution I've found was convert all ids from array do MongoId() and then apply it to the query:

public function removeCarrierAction()
{
    $request = new Phalcon\Http\Request();
    if($request->isPost() == true){
        if($request->isAjax() == true){

            //Get the array of ids, and then apply MongoId() to each one of them
            $ids = explode(",",$request->getPost(0));
            $c = count($ids);
            for($i=0;$i<$c;$i++){
                //Here comes the magic
                $ids[$i] = new MongoId($ids[$i]);
            }

            //And then do the query
            $carriers = Carriers::find([['_id' => ['$in' => $ids]]]);

            //And then proceed to delete normally
            foreach($carriers as $carrier){
                if($carrier->delete() == true){
                    echo print_r($ids);
                }else{
                    echo "error";
                }
            }
        }
    }
}


219
edited Apr '16

Hello... I know your Problem is solved. I am Not familiar with MongoDB but I found very helpful blog about MongoDB. I would like to share blog feel free to VISIT

:-)