在mongo中插入多行

时间:2018-10-12 18:40:16

标签: python python-3.x mongodb pandas pymongo

我有一个熊猫数据框,如下所示:-

enter image description here

我正在使用以下代码并将数据插入mongodb:-

mydb = conn["mydatabase"]
mycol = mydb["test"]

x = results_df["user"] # result_df is the data frame.

for item in x:
    mycol.collection.insert({"user" : item , },check_keys= False)

采用以下格式:-

{ "_id" : ObjectId("5bc0df186b3f65f926bceaeb"), "user" : ".287aa7e54ebe4088ac0a7983df4e4a28.@fnwp.vivox.com" }
{ "_id" : ObjectId("5bc0df186b3f65f926bceaec"), "user" : ".8f47cf677f9b429ab13245e12ce2fdda.@fnwp.vivox.com" }
{ "_id" : ObjectId("5bc0df186b3f65f926bceaed"), "user" : ".9ab4cdcc2cd24c9688f162817cbbbf34.@fnwp.vivox.com" }

我想在每个对象ID中插入更多行,如下所示:-

{ "_id" : ObjectId("5bc0df186b3f65f926bceaeb"), "user" : ".287aa7e54ebe4088ac0a7983df4e4a28.@fnwp.vivox.com", "ua":"Vivox-SDK-4.9.0002.29794O" , "type":"vx_pp_log"}

我想像这样插入数十亿行,并希望使其保持动态,因为将来我会添加更多行。

1 个答案:

答案 0 :(得分:0)

您在这里:-

mydb = conn["testdb"]
mycol = mydb["test"]

user =  results_df['user']
ua = results_df['ua']
time = results_df['@timestamp']

df = pd.DataFrame({'user': user, 'ua': ua, 'time': time}) # keep increasing the columns 

mycol.collection.insert(df.to_dict('records'))