MongoDB-how to import csv data into a table in mongodb database

background introduction

b4cf43b248b07ad2becd71813d143b82.png

948ddf48c5d33a09e6d6e7c9864413aa.png

The background is that the development suddenly asked me if I could import data to the database, and then only need a few columns of data.

My first thought was: read the csv file with a python script, splice the content into a text in json format, and then import it in a script. Later, I found that the GUI tool I used can directly import data into the database.

Implementation process

Since there are tools that can be imported directly, it must be imported with ready-made tools. For tools, I choose here: NoSQLBooster, official website download address: https://nosqlbooster.com/downloads

The operation steps are as follows:

1. To import data to which database, first select the database, then right-click, select Import Collections, and then select the Import from JSON and CSV files option

980025822c8115c60922483be8d70d0b.png

2. Select which columns to import on the page, and the field types of the corresponding columns, and then click Execute:

0136f99cc8434cfb7038af8969745715.png

The contents of the csv file selected here are as follows:

66585c6df0675a4af64f3427b5ea9d29.png

The effect after importing the database:

02257c07008eeca66f16bfa3d22a8dff.png

If you need to reorder the fields after importing data, you can use the following statement:

格式:
db.集合名称.update({}, {$rename:{"旧键名称":"新键名称"}}, false, true)
举例:将import_test表中的age字段改为大写的AGE字段:
db.import_test.update({}, {$rename:{"age":"AGE"}}, false, true)

9050ef8e8237925a59b8c9036ca80ed7.png

I don’t know if my careful friends have noticed two problems with the data imported above:

1. The data involves Chinese, if it is imported, it will be garbled

2. If the csv file is similar to the mobile phone number, if the format is not set in the csv, the value will change after importing it.

Since the above tool imports may sometimes be unreliable, then let's try the way of using python scripts:

Not much nonsense, just upload the code directly: ( Note that when reading the file, you must specify the encoding, otherwise there may be unknown exceptions such as reading errors/garbled characters )

import pandas as pd
from pymongo import MongoClient


# 读取CSV文件
df = pd.read_csv('import_test.csv',encoding='GBK')
print(df.to_dict('records'))
# 连接MongoDB
client = MongoClient('localhost', 27017)
db = client['study']
collection = db['import_test']


# 将数据写入MongoDB
collection.insert_many(df.to_dict('records'))

operation result:

dffc74ca374bed4523b3d99c8ac25bfb.png

The data in mongo is as follows:

aa1ea1617138bafdaca7f9f4db98651e.png

Past recommendation

MongoDB - Introduction to MongoDB

MongoDB- build a mongodb database for practice through docker

MongoDB- Install a mongodb database locally on a windows computer

MongoDB - use the mongo/mongosh command line to connect to the database

MongoDB- Quick start with some simple operations on the MongoDB command line

Introduction to the meaning of MongoDB- _id field

MongoDB- insert data insert, insertOne, insertMany, save usage introduction

Introduction to the basic usage of MongoDB- table data query

Introduction to the usage of >, >=, <, <=, =, !=, in, and not in in MongoDB- query statements

Introduction to the usage of logical operators not, and, or, and nor in MongoDB- query statements

Introduction to the use of $exists and the combination of $ne, $nin, $nor, and $not in MongoDB- query statements

MongoDB- Use $type to query whether the type of a field is xxx

Introduction to the usage of $all in MongoDB- query

MeterSphere Tutorial: python2 pre-script to check mongodb library to extract parameters & check library assertion

MongoDB- find duplicate records in the table

They are all here, let’s watch before leaving~~~

69a1e0af0e29834993a0e8eed9344f47.gif

Guess you like

Origin blog.csdn.net/liboshi123/article/details/129210991