How to add a pd column as _id for nosql collection?

Viewed 37

I have a mongodb (nosql) collection where I want to add data from a pandas dataframe. I am doing it as follow:

db.collection.insert_many(df.to_dict('records'))

This command works fine but it just add a column name '_id' by itself. My dataframe contains 3 columns named 'user_id', 'name' and 'age'.

The issue is, I want to make the 'user_id' column as the '_id' column of my nosql collection. I have been to this page but could not find the solution about a dataframe.

Is there any way to do so?

0 Answers
Related