在mong中插入多行

2024-09-23 06:28:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个熊猫数据框如下:

enter image description here

我正在使用下面的代码并将数据插入蒙哥达:你知道吗

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"}

我想插入数十亿行这样,并希望保持它的动态,因为可能在未来我会添加更多的行。你知道吗


Tags: 数据代码comiddfmyconnitem
1条回答
网友
1楼 · 发布于 2024-09-23 06:28:49

给你:-

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'))

相关问题 更多 >