A collection object is usually created by calling [[Database::getCollection()]] or [[Connection::getCollection()]].
Collection provides the basic interface for the Mongo queries, mostly: insert, update, delete operations.
For example:
php
$collection = Yii::$app->mongodb->getCollection('customer');
$collection->insert(['name' => 'John Smith', 'status' => 1]);
Collection also provides shortcut for Command methods, such as Collection::group, Collection::mapReduce and so on.
To perform "find" queries, please use Query instead.
/** * Removes data from the collection. * @param array $condition description of records to remove. * @param array $options list of options in format: optionName => optionValue. * @return integer|boolean number of updated documents or whether operation was successful. * @throws Exception on failure. */ public function remove($condition = [], $options = []) { $result = parent::remove($condition, $options); $this->tryLastError(); // MongoGridFS::remove will return even if the remove failed return $result; }
/** * @inheritdoc * @return Cursor cursor for the search results */ public function find($condition = [], $fields = [], $options = []) { return new Cursor($this, parent::find($condition, $fields, $options)); }
/** * Finds all records in collection. * @param \yii\mongodb\Collection $collection * @param array $condition * @param array $fields * @return array rows */ protected function findAll($collection, $condition = [], $fields = []) { $cursor = $collection->find($condition, $fields); $result = []; foreach ($cursor as $data) { $result[] = $data; } return $result; }