如何使用时间序列数据创建猫鼬模式

时间:2019-01-17 19:36:47

标签: node.js mongodb mongoose

我正在尝试时间序列数据的架构。但是我无法保存列值数据。因此,我想清楚地定义值,而不仅仅是空对象。因此,我在网上查看了有关信息,并在link上获得了有关mongo db的时间序列的信息。但是我无法创建完美的架构。

我真的需要别人的帮助来解决此问题。我尝试了很多事情,并且只是按照自己了解的方式进行。

我正在尝试插入如下代码:

const symbol = 'WLTW';
const date = '2016-01-05 00:00:00';
const open = 123.43;
const close = 125.839996;
const low = 122.309998;
const high = 126.25;
const volume = 2163600.0;
const month = 1;
const day = 5;

const stock = new Stock({
  date,
  symbol,
  values: { [month]: { [day]: { open, close, low, high, volume } } }
});

stock
  .save()
  .then(res => `Result = ${res}`)
  .catch(e => `Error = ${e}`);


const stockSchema = new Schema({
  symbol: {
    type: String,
    required: true
  },
  date: { type: Date },
  values: {
    month: {
      day: {
        open: { type: Number },
        close: { type: Number },
        low: { type: Number },
        high: { type: Number },
        volume: { type: Number }
       }
    }
  }
});

实际情况如何:

{
    "_id": {
        "$oid": "5c40d7b655f7836a7fd4e3cf"
    },
    "date": {
        "$date": "2016-01-04T18:30:00.000Z"
    },
    "symbol": "WLTW",
    "__v": 0
}

但是,如果我在给values: {}的架构中进行了更改,那么我无法在数据库中获得正确的数据:

{
    "_id": {
        "$oid": "5c40d2e6a73ca369f61de9a3"
    },
    "date": {
        "$date": "2016-01-04T18:30:00.000Z"
    },
    "symbol": "WLTW",
    "values": {
        "1": {
            "5": {
                "open": 123.43,
                "close": 125.839996,
                "low": 122.309998,
                "high": 126.25,
                "volume": 2163600
            }
        }
    },
    "__v": 0
}

1 个答案:

答案 0 :(得分:0)

在阅读了几篇文章并搜索了许多天之后。我知道最佳实践是将任何动态数据都保留在字段名称之外。

因此,我决定这样做并更新了架构,以使现在的字段名称不再是动态数据。

class ProspektDateType extends AbstractType
{

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('date',
                DateType::class,
                array(

                    'attr' => [
                        'class' => 'form-control input-inline date_custom input-sm date_et',
                        'html5' => false,
                        'readonly' => true

                    ],
                    'required' => false,
                    'widget'        => 'single_text',
                    'format'        => 'ccc, dd.MM.y',
                    'label' => 'ET',
                    'model_timezone' => 'Europe/Berlin',

                )
            );
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => ProspektDates::class,
            'error_bubbling'=>false
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'prospektDates';
    }
}

然后表视图将是:

class ProspektLaendervariantenType extends AbstractType

{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('prospektDates', CollectionType::class, [
            'label'         => 'ETs',
            'entry_type'    => ProspektDateType::class,
            'entry_options' => [
                'label' => false,
                'attr' => [
                    'class' => 'date-item', 
                ],
            ],
            'allow_add'     => true,
            'allow_delete'  => true,
            'prototype'     => true,
            'required'      => false,
           'by_reference'  => false,
            'delete_empty'  => true,
           'prototype_name' => '__prospektDates-collection__',

           'attr'          => [
                'class' => 'prospektDates-collection',
            ],
           'block_name' => 'prospektDates',

       ]);

    }



    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'ProspektLaendervarianten::class',
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'laendervarianten';
    }
}