MongoDB PHP insert into sub-array -
ok, not sure if mongodb can this, need following json inserted currency db.
the part want update exchangehistory, need keep history of exchange rates day. , next day e.g.
for e.g
{"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"}]}]}}
i not store in array this. create flat data structure has:
{ 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, } this lot lighter on database, documents never grow. , if documents grow have moved. can query current rate:
$collection->find( array( 'from' => 'usd', 'to' => 'eur' ) ) ->sort( 'updated' => -1 )->limit( 1 ); and accessing historical information:
$collection->find( array( 'from' => 'usd', 'to' => 'eur' ) ) ->sort( 'updated' => -1 )->skip( 1 );
Comments
Post a Comment