简体   繁体   中英

PyMongo: update many, pandas Dataframe

I am trying to store technical indicator values into the same collection that I have price records. For instance, if the dataframe that is currently stored in a collection in MongoDB looks like this:

   Unix Timestamp                 Date  Symbol  ...       Low     Close     Volume
1   1562540400000  2019-07-07 23:00:00  BTCUSD  ...  11423.00  11475.07  32.996559
2   1562536800000  2019-07-07 22:00:00  BTCUSD  ...  11333.59  11423.00  48.937730
3   1562533200000  2019-07-07 21:00:00  BTCUSD  ...  11478.20  11526.25  25.323908
4   1562529600000  2019-07-07 20:00:00  BTCUSD  ...  11423.94  11515.80  63.211972

and a search for what is stored yeilds:

{'_id': ObjectId('5d30bc55ed1bb60762ac4d8d'), 'Unix Timestamp': 1562544000000, 'Date': '2019-07-08 00:00:00', 'Symbol': 'BTCUSD', 'Open': 11475.07, 'High': 11540.33, 'Low': 11469.53, 'Close': 11506.43, 'Volume': 10.77073088}
{'_id': ObjectId('5d30bc55ed1bb60762ac4d8e'), 'Unix Timestamp': 1562540400000, 'Date': '2019-07-07 23:00:00', 'Symbol': 'BTCUSD', 'Open': 11423.0, 'High': 11482.72, 'Low': 11423.0, 'Close': 11475.07, 'Volume': 32.99655899}
{'_id': ObjectId('5d30bc55ed1bb60762ac4d8f'), 'Unix Timestamp': 1562536800000, 'Date': '2019-07-07 22:00:00', 'Symbol': 'BTCUSD', 'Open': 11526.25, 'High': 11572.74, 'Low': 11333.59, 'Close': 11423.0, 'Volume': 48.9377301868}
{'_id': ObjectId('5d30bc55ed1bb60762ac4d90'), 'Unix Timestamp': 1562533200000, 'Date': '2019-07-07 21:00:00', 'Symbol': 'BTCUSD', 'Open': 11515.8, 'High': 11562.65, 'Low': 11478.2, 'Close': 11526.25, 'Volume': 25.3239076786}
{'_id': ObjectId('5d30bc55ed1bb60762ac4d91'), 'Unix Timestamp': 1562529600000, 'Date': '2019-07-07 20:00:00', 'Symbol': 'BTCUSD', 'Open': 11547.98, 'High': 11624.88, 'Low': 11423.94, 'Close': 11515.8, 'Volume': 63.211972440299995}

Is there a way to update multiple documents with their associated technical indicators? For instance, that dataframe/series could look like:

    EMA26     ATR
0  11506.430000   70.80
1  11490.454151   83.43
2  11467.115719  239.15
3  11482.746419  139.65
4  11489.865287  200.94

The only thing I can think of is just iterating over each row in the technical indicators dataframe and update based on the unix timestamp or something.

I think you will have to iterate each document.

I looked for info, but the Collection class in PyMongo Documenttion shows no clue to create a bulk. Instead, it only lets you update a bunch of documents that match a single condition (update_many).

Assuming that your timestamps are unique, there should not be additional issues using update_one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM