1

I have a document structure like

{
  "startDate": ISODate("2015-01-01T00:00:00Z"),
  "endDate" : ISODate("2015-01-10T00:00:00Z"),
  "foo" : "bar"
}

Is it possible to expand the date range like this?

{
 "dates": [
           ISODate("2015-01-01T00:00:00Z"),
           ISODate("2015-01-02T00:00:00Z"),
           ISODate("2015-01-03T00:00:00Z"),
           ISODate("2015-01-04T00:00:00Z"),
           ISODate("2015-01-05T00:00:00Z"),
           ISODate("2015-01-06T00:00:00Z"),
           ISODate("2015-01-07T00:00:00Z"),
           ISODate("2015-01-08T00:00:00Z"),
           ISODate("2015-01-09T00:00:00Z"),
           ISODate("2015-01-10T00:00:00Z")
        ]
}
Community
  • 1
  • 1
Rnet
  • 4,796
  • 9
  • 47
  • 83
  • you did not specify the driver language to use. But get the document start and end dates, create a new document that has an array of dates which starts from the start date and increment the next date in the array by one day until you reach the end date. Delete the old document and insert the new one to replace it. – faljbour Apr 16 '15 at 05:28
  • or construct the array from the start and end dates, add it as a new json array to the current document and delete the start and end dates and foo if it is not needed – faljbour Apr 16 '15 at 05:31
  • @Rnet this not possible through mongo query you need to set some programming skills to expands this or try to follow **Salvador Dali** answer. – Neo-coder Apr 16 '15 at 05:54

1 Answers1

1

As far as I understood you want to add field dates for all your documents. Here is an approach I would use (you can do this in mongoshell):

1) iterate over all the documents modifying them

db.coll.find()..snapshot().forEach(function(o){
    o.dates = func(o.startDate, o.endDate);
    db.coll.save(o);
});

2) where you function func is something similar to this answer (you need to modify it a little bit because it looks like you need only dates without time included there.

Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753