class Iro::Priceitem

#
# Priceitems are intra-day! See Datapoint for daily data
# Specifically Option or Stock priceitem?
#

def self.my_find props={}

def self.my_find props={}
  lookup = { '$lookup': {
    'from':         'iro_price_items',
    'localField':   'date',
    'foreignField': 'date',
    'pipeline': [
      { '$sort': { 'value': -1 } },
    ],
    'as':           'dates',
  } }
  lookup_merge = { '$replaceRoot': {
    'newRoot': { '$mergeObjects': [
      { '$arrayElemAt': [ "$dates", 0 ] }, "$$ROOT"
    ] }
  } }
  match = { '$match': {
    'date': {
      '$gte': props[:begin_on],
      '$lte': props[:end_on],
    }
  } }
  group = { '$group': {
    '_id': "$date",
    'my_doc': { '$first': "$$ROOT" }
  } }
  outs = Iro::Date.collection.aggregate([
    match,
    lookup,
    lookup_merge,
    group,
    { '$replaceRoot': { 'newRoot': "$my_doc" } },
    # { '$replaceRoot': { 'newRoot': "$my_doc" } },
    { '$project': { '_id': 0, 'date': 1, 'value': 1 } },
    { '$sort': { 'date': 1 } },
  ])
  puts! 'result'
  pp outs.to_a
  # puts! outs.to_a, 'result'
end