Doctrine\MongoDB\Query\Query::execute PHP Method

execute() public method

The return value will vary based on the query type. Commands with results (e.g. aggregate, inline mapReduce) may return an ArrayIterator. Other commands and operations may return a status array or a boolean, depending on the driver's write concern. Queries and some mapReduce commands will return a CursorInterface.
public execute ( ) : mixed
return mixed
    public function execute()
    {
        $options = $this->options;
        switch ($this->query['type']) {
            case self::TYPE_FIND:
                $cursor = $this->collection->find($this->query['query'], isset($this->query['select']) ? $this->query['select'] : []);
                return $this->prepareCursor($cursor);
            case self::TYPE_FIND_AND_UPDATE:
                $queryOptions = $this->getQueryOptions('new', 'select', 'sort', 'upsert');
                $queryOptions = $this->renameQueryOptions($queryOptions, ['select' => 'fields']);
                return $this->collection->findAndUpdate($this->query['query'], $this->query['newObj'], array_merge($options, $queryOptions));
            case self::TYPE_FIND_AND_REMOVE:
                $queryOptions = $this->getQueryOptions('select', 'sort');
                $queryOptions = $this->renameQueryOptions($queryOptions, ['select' => 'fields']);
                return $this->collection->findAndRemove($this->query['query'], array_merge($options, $queryOptions));
            case self::TYPE_INSERT:
                return $this->collection->insert($this->query['newObj'], $options);
            case self::TYPE_UPDATE:
                return $this->collection->update($this->query['query'], $this->query['newObj'], array_merge($options, $this->getQueryOptions('multiple', 'upsert')));
            case self::TYPE_REMOVE:
                return $this->collection->remove($this->query['query'], $options);
            case self::TYPE_GROUP:
                if (!empty($this->query['query'])) {
                    $options['cond'] = $this->query['query'];
                }
                $collection = $this->collection;
                $query = $this->query;
                $closure = function () use($collection, $query, $options) {
                    return $collection->group($query['group']['keys'], $query['group']['initial'], $query['group']['reduce'], array_merge($options, $query['group']['options']));
                };
                return $this->withReadPreference($collection->getDatabase(), $closure);
            case self::TYPE_MAP_REDUCE:
                if (isset($this->query['limit'])) {
                    $options['limit'] = $this->query['limit'];
                }
                $collection = $this->collection;
                $query = $this->query;
                $closure = function () use($collection, $query, $options) {
                    return $collection->mapReduce($query['mapReduce']['map'], $query['mapReduce']['reduce'], $query['mapReduce']['out'], $query['query'], array_merge($options, $query['mapReduce']['options']));
                };
                $results = $this->withReadPreference($collection->getDatabase(), $closure);
                return $results instanceof Cursor ? $this->prepareCursor($results) : $results;
            case self::TYPE_DISTINCT:
                $collection = $this->collection;
                $query = $this->query;
                $closure = function () use($collection, $query, $options) {
                    return $collection->distinct($query['distinct'], $query['query'], $options);
                };
                return $this->withReadPreference($collection->getDatabase(), $closure);
            case self::TYPE_GEO_NEAR:
                if (isset($this->query['limit'])) {
                    $options['num'] = $this->query['limit'];
                }
                $collection = $this->collection;
                $query = $this->query;
                $closure = function () use($collection, $query, $options) {
                    return $collection->near($query['geoNear']['near'], $query['query'], array_merge($options, $query['geoNear']['options']));
                };
                return $this->withReadPreference($collection->getDatabase(), $closure);
            case self::TYPE_COUNT:
                $collection = $this->collection;
                $query = $this->query;
                $closure = function () use($collection, $query, $options) {
                    return $collection->count($query['query'], array_merge($options, $this->getQueryOptions('hint', 'limit', 'maxTimeMS', 'skip')));
                };
                return $this->withReadPreference($collection, $closure);
        }
    }

Usage Example

 function let(DocumentManager $manager, ChannelInterface $ecommerce, ChannelInterface $mobile, LocaleInterface $enUs, LocaleInterface $frFr, CategoryInterface $category, ChannelManager $channelManager, CategoryRepositoryInterface $categoryRepository, ProductRepository $productRepository, QueryBuilder $ormQb, Builder $odmQb, Query $odmQuery, Cursor $cursor)
 {
     $enUs->getCode()->willReturn('en_US');
     $frFr->getCode()->willReturn('fr_FR');
     $ecommerce->getCode()->willReturn('ecommerce');
     $ecommerce->getLabel()->willReturn('ECommerce');
     $ecommerce->getLocales()->willReturn(array($enUs, $frFr));
     $ecommerce->getCategory()->willReturn($category);
     $mobile->getCode()->willReturn('mobile');
     $mobile->getLabel()->willReturn('Mobile');
     $mobile->getLocales()->willReturn(array($enUs));
     $mobile->getCategory()->willReturn($category);
     $odmQuery->execute()->willReturn($cursor);
     $productRepository->createQueryBuilder()->willReturn($odmQb);
     $odmQb->hydrate(Argument::any())->willReturn($odmQb);
     $odmQb->field(Argument::any())->willReturn($odmQb);
     $odmQb->in(Argument::any())->willReturn($odmQb);
     $odmQb->equals(Argument::any())->willReturn($odmQb);
     $odmQb->select('_id')->willReturn($odmQb);
     $odmQb->getQuery()->willReturn($odmQuery);
     $categoryRepository->getAllChildrenQueryBuilder($category, true)->willReturn($ormQb);
     $categoryRepository->getCategoryIds($category, $ormQb)->willReturn(array(1, 2, 3));
     $channelManager->getFullChannels()->willReturn(array($ecommerce, $mobile));
     $manager->getRepository('pim_product_class')->willReturn($productRepository);
     $this->beConstructedWith($manager, $channelManager, $categoryRepository, 'pim_product_class');
 }
All Usage Examples Of Doctrine\MongoDB\Query\Query::execute