yiisoft / yii2-mongodb

Yii 2 MongoDB extension
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
326 stars 191 forks source link

MongoDB DropIndexes() fails when there is no index #286

Closed xiaoyi510 closed 4 years ago

xiaoyi510 commented 4 years ago

What steps will reproduce the problem?

What's expected?

What do you get instead?

Additional info

Q A
Yii version 2
Yii MongoDB version 2.1.7
MongoDB server version 4.x
PHP version 7.2
Operating system LINUX

MongoDB DropIndexes() fails when there is no index

\yii\mongodb\Collection::dropAllIndexes
/**
     * Drops all indexes for this collection.
     * @throws Exception on failure.
     * @return int count of dropped indexes.
     */
    public function dropAllIndexes()
    {
        $result = $this->database->createCommand()->dropIndexes($this->name, '*');
        return $result['nIndexesWas'];
    }

nIndexesWas index does not return this field when deleting a collection without an index

Should be

  public function dropAllIndexes()
    {
        $result = $this->database->createCommand()->dropIndexes($this->name, '*');
        return $result['nIndexesWas']??0;
    }
samdark commented 4 years ago

Looks valid. Do you want to prepare a pull request?