0

So the problem is:

I have next structure:

    "details" : {
        "2015-08-17" : {
            "date" : ISODate("2015-08-17T08:15:05.479Z"),
             ...
        },
        "2015-08-18" : {
            "date" : ISODate("2015-08-17T08:15:05.479Z"),
             ...
        },
        ...
    }

I have to get all date fields. How can I got it?

I need solution with aggregation framework, because do it by cursor spend much more time (sure, I can use forEach and then with for get all that I need, but it's not a good idea).

I found solution with $objectToArray link, but I have only mongo version 3.2. So I want very much to see you advices ;) and will be very grateful.

UPDATE

THE QUESTION: how can get list of dates like below?

{
  dates: [
     ISODate("2015-08-17T08:15:05.479Z"),
     ISODate("2015-08-17T08:15:05.479Z"),
     ISODate("2015-08-17T08:15:05.479Z"),
     ...
  ]
}
  • Well you're not going to get a solution for the aggregation framework because without that operator it has no way of traversing the keys in your documents. In all honesty, even with an updated version of MongoDB, storing "named keys" is still a a really bad practice. This really should be an a array, and you also gain absolutely nothing by naming keys based on the "date portion" of the timestamp value only. You can always extract or even effectively "range query" on the plain BSON date values. So the structure is actually wasteful and should be changed. – Neil Lunn Aug 17 '17 at 10:58
  • You can always extract or even effectively "range query" on the plain BSON date values - could you can present me example? I have an ArrayCollection in Symfony and the key is very useful for me. So I can't refuse of this structure. And one more: can I get just array without keys by some functions? – Nikita_kharkov_ua Aug 17 '17 at 11:54
  • Example of what exactly? You actually have not even asked a question here at all. All you really say is you *"want to get all the date fields"* but you don't explain for what purpose or whether you are supposed to select within a range or anything at all. You could therefore probably spend less time on talking about `$objectToArray` which you know you "cannot" use, and instead describing what it is that you actually need to do, and to what purpose. The only other clear thing I added was that the structure you have serves no purpose. But besides that, you never even asked a question. – Neil Lunn Aug 17 '17 at 11:59
  • I'm sorry, I really thought that question is quite simple and clear... Question was updated. You wrote `You can always extract or even effectively "range query" on the plain BSON date values`. I was interested by word `extract`. How can I do it with such structure? Thanks. – Nikita_kharkov_ua Aug 18 '17 at 05:31
  • @Nikita_kharkov_ua Hi. Even I'm facing a similar issue. I'm not able to find the equivalent of $objectToArray in symfony. If you have found a solution o this, can you post it here? If you haven't can you post what alternative you have done? – Jason Mar 09 '18 at 12:44
  • 1
    Hi. Injecting $objectToArray solved my issue. $builder->addFields() ->field('array') ->expression(['$objectToArray' => '$object']) (Refered https://github.com/doctrine/mongodb-odm/issues/1677 ) – Jason Mar 09 '18 at 12:53
  • So it's remain to move to upper version! :) Thanks for your comments ;) Don't forget to up the question raiting! – Nikita_kharkov_ua Mar 12 '18 at 12:28

0 Answers0