2

So, the problem is to get ids (or any another fields) in one array, like:

[
    "someId1",
    "someId2",
    "someId3",
]

I tried to take it by next query:

$this->createQueryBuilder()
        ->select('_id')
        ->hydrate(false)
        ->getQuery()
        ->toArray()

but I took not what I want, but near this:

112 => array:1 [
  "_id" => 112
]
113 => array:1 [
  "_id" => 113
]
114 => array:1 [
  "_id" => 114
]

(I use strategy INCREMENT). I tried do this by map, reduce, but I understood that I know nothing in these functions :). So can somebody help me?

P.S: I know how to realise this with php functions, like array_keys | array_values. But I really want to know how to do it by mongoDB, and particularly, in ODM with createQueryBuilder

  • This is ORM so try to adapt this to your ODM: In repo `public function findIds() { return $this->createQueryBuilder('c') ->select('partial c.{id}') ->getQuery() ->getResult(Query::HYDRATE_ARRAY); }` then `$ids = $this->yourRepository->findIds(); $result = array_column($ids, 'id');` in your service. – BentCoder Mar 15 '17 at 19:56
  • ORM and ODM **NOT** is the same. Just read the documentation the core of this projects – Nikita_kharkov_ua Mar 16 '17 at 21:36

1 Answers1

0

This usually can be done by creating a custom hydrator. You could take a look at this example and try to adapt it to ODM https://gist.github.com/Tom32i/7984541

Renan Taranto
  • 562
  • 4
  • 8
  • The hydration in ODM has only 2 params: **true, false**, that means result in object view or in array view - that's it. So I think its not useful advice. – Nikita_kharkov_ua Mar 16 '17 at 21:39
  • @Nikita_kharkov_ua Are you sure? So why there's a hydrator interface http://www.doctrine-project.org/api/mongodb_odm/1.0/class-Doctrine.ODM.MongoDB.Hydrator.HydratorInterface.html and an option to set Hydrators namespace http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html#setup ? – Renan Taranto Mar 16 '17 at 21:54
  • You open my eyes! But! No one usage example for this - this is the **first** important thing. **Second** - no such method like in ORM setHydrator. Only **hydrate(true | false)**. Now I have similar task with hydration: get object with reference, and in array of references must be only ids, not full object. Sure, I can get object and map it to take needle values. But I fully sure that mongoDB support fetching results in custom represent. – Nikita_kharkov_ua Mar 17 '17 at 11:40
  • https://github.com/doctrine/mongodb-odm/issues/1617#issuecomment-315141027 - discussing about this problem – Nikita_kharkov_ua Jul 14 '17 at 13:45