1

好的,不确定 mongodb 是否可以做到这一点,但我需要将以下 JSON 插入到我的货币数据库中。

我们要更新的部分是exchangehistory,我们需要保留当天的所有汇率历史。第二天,例如

例如

{"from":"USD","currentexchange":[{"to":"NZD","rate":"1.3194","updated":"6\/5\/20121:38am"},{"to":"KWD","rate":"0.2807","updated":"6\/5\/20121:38am"},{"to":"GBP","rate":"0.6495","updated":"6\/5\/20121:38am"},{"to":"AUD","rate":"1.0228","updated":"6\/5\/20121:38am"}],"exchangehistory":{"6\/5\/2012":[{"1:38am":[{"to":"NZD","rate":"1.3194","updated":"1:38am"}]},{"1:38am":[{"to":"KWD","rate":"0.2807","updated":"1:38am"}]},{"1:38am":[{"to":"GBP","rate":"0.6495","updated":"1:38am"}]},{"1:38am":[{"to":"AUD","rate":"1.0228","updated":"1:38am"}]}]}}
4

2 回答 2

0

我很可能不会将它存储在这样的数组中。我将创建一个平面数据结构,它具有:

{
    from: "USD",
    to: "EUR",
    updated: new DateTime("2012-05-04 13:43"),
    rate: 1.235,
},
{
    from: "USD",
    to: "EUR",
    updated: new DateTime("2012-05-06 13:43"),
    rate: 1.24,
},
{
    from: "USD",
    to: "AUD",
    updated: new DateTime("2012-05-06 13:43"),
    rate: 1.43,
}

这在数据库上要轻得多,因为文档永远不会增长。如果文档增长,它们必须被移动。您还可以非常轻松地查询当前汇率:

$collection->find( array( 'from' => 'USD', 'to' => 'EUR' ) )
           ->sort( 'updated' => -1 )->limit( 1 );

并访问所有历史信息:

$collection->find( array( 'from' => 'USD', 'to' => 'EUR' ) )
           ->sort( 'updated' => -1 )->skip( 1 );
于 2012-06-05T11:47:46.323 回答
-1

这不适用于 PHP,但它可能有用。您必须从您的 json 字符串中创建一个 BSON 对象才能存储在数据库中。 从 JSON 创建 BSON 您也可以使用 MongoDB PHP 驱动程序教程和文档。它们可以在这里找到:MongoDB PHP 驱动程序教程

于 2012-06-05T05:56:37.373 回答