简体   繁体   中英

Why the delete() method of Phalcon\Mvc\Collection in the MongoDB collection may not work?

I inserted three documents to mongoDb collection. I want to delete a document from collection.:

It is mongoDB model:

use \Phalcon\Mvc\MongoCollection;

class AutoSnippet extends MongoCollection
{
  public $name;
  public $snippets = [];

  public function onConstruct()
  {
    $this->setSource('AutoSnippet');
  }

  public function initialize()
  {
    $this->setConnectionService('mongodbTracker');
    $this->getConnection()->selectCollection('AutoSnippet');
  }
}

Both of them work successfully.

1)

$snippet = AutoSnippet::findById("5e80a9d2577d257fe9703314");

2)

$snippet = AutoSnippet::findFirst([[
      '_id' => new MongoDB\BSON\ObjectID("5e80a9d2577d257fe9703314")
    ]]);

but delete() method doesn't work:

$snippet->delete(); // returns true

delete() method returns true but document with id "5e80a9d2577d257fe9703314" is still there.

The problem was in the name of the collection. The document was not deleted due to the fact that I named the collection in uppercase. I changed the name of the collection to "auto_snippet" and the removal worked.

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