Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Menu Docs
Página inicial do Docs
/
Ferramentas de banco de dados
/

Guia de exemplo do mongoimport

O MongoDB fornece a ferramenta mongoimport para importar arquivos JSON e CSV para seu conjunto de dados principal. Este guia explica como usar efetivamente o mongoimport para mover seus dados para o banco de dados MongoDB.

  • Comece com o Atlas.

  • Siga o Guia de Instalação de Database Tools para instalar o mongoimport.

  • Crie os arquivos de exemplo .

    1
    mkdir test-datasets
    cd test-datasets
    2
    @'
    {
    "tripduration": 602,
    "starttime": "2019-12-01 00:00:05.5640",
    "stoptime": "2019-12-01 00:10:07.8180",
    "start station id": 3382,
    "start station name": "Carroll St & Smith St",
    "start station latitude": 40.680611,
    "start station longitude": -73.99475825,
    "end station id": 3304,
    "end station name": "6 Ave & 9 St",
    "end station latitude": 40.668127,
    "end station longitude": -73.98377641,
    "bikeid": 41932,
    "usertype": "Subscriber",
    "birth year": 1970,
    "gender": "male"
    }
    '@ | Set-Content -Path ride_01.json
    @'
    {
    "tripduration": 1206,
    "starttime": "2019-12-01 00:00:10.9630",
    "stoptime": "2019-12-01 00:20:17.8820",
    "start station id": 362,
    "start station name": "Broadway & W 37 St",
    "start station latitude": 40.75172632,
    "start station longitude": -73.98753523,
    "end station id": 500,
    "end station name": "Broadway & W 51 St",
    "end station latitude": 40.76228826,
    "end station longitude": -73.98336183,
    "bikeid": 18869,
    "usertype": "Customer",
    "birth year": 1999,
    "gender": "male"
    }
    '@ | Set-Content -Path ride_02.json
    @'
    {
    "tripduration": 723,
    "starttime": "2019-12-01 00:00:11.8180",
    "stoptime": "2019-12-01 00:12:14.8310",
    "start station id": 146,
    "start station name": "Hudson St & Reade St",
    "start station latitude": 40.71625008,
    "start station longitude": -74.0091059,
    "end station id": 238,
    "end station name": "Bank St & Washington St",
    "end station latitude": 40.7361967,
    "end station longitude": -74.00859207,
    "bikeid": 15334,
    "usertype": "Subscriber",
    "birth year": 1997,
    "gender": "male"
    }
    '@ | Set-Content -Path ride_03.json
    @'
    [
    {
    "tripduration": 602,
    "starttime": "2019-12-01 00:00:05.5640",
    "stoptime": "2019-12-01 00:10:07.8180",
    "start station id": 3382,
    "start station name": "Carroll St & Smith St",
    "start station latitude": 40.680611,
    "start station longitude": -73.99475825,
    "end station id": 3304,
    "end station name": "6 Ave & 9 St",
    "end station latitude": 40.668127,
    "end station longitude": -73.98377641,
    "bikeid": 41932,
    "usertype": "Subscriber",
    "birth year": 1970,
    "gender": "male"
    },
    {
    "tripduration": 1206,
    "starttime": "2019-12-01 00:00:10.9630",
    "stoptime": "2019-12-01 00:20:17.8820",
    "start station id": 362,
    "start station name": "Broadway & W 37 St",
    "start station latitude": 40.75172632,
    "start station longitude": -73.98753523,
    "end station id": 500,
    "end station name": "Broadway & W 51 St",
    "end station latitude": 40.76228826,
    "end station longitude": -73.98336183,
    "bikeid": 18869,
    "usertype": "Customer",
    "birth year": 1999,
    "gender": "male"
    },
    {
    "tripduration": 723,
    "starttime": "2019-12-01 00:00:11.8180",
    "stoptime": "2019-12-01 00:12:14.8310",
    "start station id": 146,
    "start station name": "Hudson St & Reade St",
    "start station latitude": 40.71625008,
    "start station longitude": -74.0091059,
    "end station id": 238,
    "end station name": "Bank St & Washington St",
    "end station latitude": 40.7361967,
    "end station longitude": -74.00859207,
    "bikeid": 15334,
    "usertype": "Subscriber",
    "birth year": 1997,
    "gender": "male"
    }
    ]
    '@ | Set-Content -Path rides.json
    @'
    602,"2019-12-01 00:00:05.5640","2019-12-01 00:10:07.8180",3382,"Carroll St & Smith St",40.680611,-73.99475825,3304,"6 Ave & 9 St",40.668127,-73.98377641,41932,"Subscriber",1970,1
    1206,"2019-12-01 00:00:10.9630","2019-12-01 00:20:17.8820",362,"Broadway & W 37 St",40.75172632,-73.98753523,500,"Broadway & W 51 St",40.76228826,-73.98336183,18869,"Customer",1999,1
    723,"2019-12-01 00:00:11.8180","2019-12-01 00:12:14.8310",146,"Hudson St & Reade St",40.71625008,-74.0091059,238,"Bank St & Washington St",40.7361967,-74.00859207,15334,"Subscriber",1997,1
    '@ | Set-Content -Path rides_no_header.json
    1
    mkdir test-datasets
    cd test-datasets
    2
    echo '{
    "tripduration": 602,
    "starttime": "2019-12-01 00:00:05.5640",
    "stoptime": "2019-12-01 00:10:07.8180",
    "start station id": 3382,
    "start station name": "Carroll St & Smith St",
    "start station latitude": 40.680611,
    "start station longitude": -73.99475825,
    "end station id": 3304,
    "end station name": "6 Ave & 9 St",
    "end station latitude": 40.668127,
    "end station longitude": -73.98377641,
    "bikeid": 41932,
    "usertype": "Subscriber",
    "birth year": 1970,
    "gender": "male"
    }' > ride_01.json
    echo '{
    "tripduration": 1206,
    "starttime": "2019-12-01 00:00:10.9630",
    "stoptime": "2019-12-01 00:20:17.8820",
    "start station id": 362,
    "start station name": "Broadway & W 37 St",
    "start station latitude": 40.75172632,
    "start station longitude": -73.98753523,
    "end station id": 500,
    "end station name": "Broadway & W 51 St",
    "end station latitude": 40.76228826,
    "end station longitude": -73.98336183,
    "bikeid": 18869,
    "usertype": "Customer",
    "birth year": 1999,
    "gender": "male"
    }' > ride_02.json
    echo '{
    "tripduration": 723,
    "starttime": "2019-12-01 00:00:11.8180",
    "stoptime": "2019-12-01 00:12:14.8310",
    "start station id": 146,
    "start station name": "Hudson St & Reade St",
    "start station latitude": 40.71625008,
    "start station longitude": -74.0091059,
    "end station id": 238,
    "end station name": "Bank St & Washington St",
    "end station latitude": 40.7361967,
    "end station longitude": -74.00859207,
    "bikeid": 15334,
    "usertype": "Subscriber",
    "birth year": 1997,
    "gender": "male"
    }' > ride_03.json
    echo '[
    {
    "tripduration": 602,
    "starttime": "2019-12-01 00:00:05.5640",
    "stoptime": "2019-12-01 00:10:07.8180",
    "start station id": 3382,
    "start station name": "Carroll St & Smith St",
    "start station latitude": 40.680611,
    "start station longitude": -73.99475825,
    "end station id": 3304,
    "end station name": "6 Ave & 9 St",
    "end station latitude": 40.668127,
    "end station longitude": -73.98377641,
    "bikeid": 41932,
    "usertype": "Subscriber",
    "birth year": 1970,
    "gender": "male"
    },
    {
    "tripduration": 1206,
    "starttime": "2019-12-01 00:00:10.9630",
    "stoptime": "2019-12-01 00:20:17.8820",
    "start station id": 362,
    "start station name": "Broadway & W 37 St",
    "start station latitude": 40.75172632,
    "start station longitude": -73.98753523,
    "end station id": 500,
    "end station name": "Broadway & W 51 St",
    "end station latitude": 40.76228826,
    "end station longitude": -73.98336183,
    "bikeid": 18869,
    "usertype": "Customer",
    "birth year": 1999,
    "gender": "male"
    },
    {
    "tripduration": 723,
    "starttime": "2019-12-01 00:00:11.8180",
    "stoptime": "2019-12-01 00:12:14.8310",
    "start station id": 146,
    "start station name": "Hudson St & Reade St",
    "start station latitude": 40.71625008,
    "start station longitude": -74.0091059,
    "end station id": 238,
    "end station name": "Bank St & Washington St",
    "end station latitude": 40.7361967,
    "end station longitude": -74.00859207,
    "bikeid": 15334,
    "usertype": "Subscriber",
    "birth year": 1997,
    "gender": "male"
    }
    ]' > rides.json
    echo '602,"2019-12-01 00:00:05.5640","2019-12-01 00:10:07.8180",3382,"Carroll St & Smith St",40.680611,-73.99475825,3304,"6 Ave & 9 St",40.668127,-73.98377641,41932,"Subscriber",1970,1
    1206,"2019-12-01 00:00:10.9630","2019-12-01 00:20:17.8820",362,"Broadway & W 37 St",40.75172632,-73.98753523,500,"Broadway & W 51 St",40.76228826,-73.98336183,18869,"Customer",1999,1
    723,"2019-12-01 00:00:11.8180","2019-12-01 00:12:14.8310",146,"Hudson St & Reade St",40.71625008,-74.0091059,238,"Bank St & Washington St",40.7361967,-74.00859207,15334,"Subscriber",1997,1
    ' > rides_no_header.json
    1
    mkdir test-datasets
    cd test-datasets
    2
    cat << 'EOF' > ride_01.json
    {
    "tripduration": 602,
    "starttime": "2019-12-01 00:00:05.5640",
    "stoptime": "2019-12-01 00:10:07.8180",
    "start station id": 3382,
    "start station name": "Carroll St & Smith St",
    "start station latitude": 40.680611,
    "start station longitude": -73.99475825,
    "end station id": 3304,
    "end station name": "6 Ave & 9 St",
    "end station latitude": 40.668127,
    "end station longitude": -73.98377641,
    "bikeid": 41932,
    "usertype": "Subscriber",
    "birth year": 1970,
    "gender": "male"
    }
    EOF
    cat << 'EOF' > ride_02.json
    {
    "tripduration": 1206,
    "starttime": "2019-12-01 00:00:10.9630",
    "stoptime": "2019-12-01 00:20:17.8820",
    "start station id": 362,
    "start station name": "Broadway & W 37 St",
    "start station latitude": 40.75172632,
    "start station longitude": -73.98753523,
    "end station id": 500,
    "end station name": "Broadway & W 51 St",
    "end station latitude": 40.76228826,
    "end station longitude": -73.98336183,
    "bikeid": 18869,
    "usertype": "Customer",
    "birth year": 1999,
    "gender": "male"
    }
    EOF
    cat << 'EOF' > ride_03.json
    {
    "tripduration": 723,
    "starttime": "2019-12-01 00:00:11.8180",
    "stoptime": "2019-12-01 00:12:14.8310",
    "start station id": 146,
    "start station name": "Hudson St & Reade St",
    "start station latitude": 40.71625008,
    "start station longitude": -74.0091059,
    "end station id": 238,
    "end station name": "Bank St & Washington St",
    "end station latitude": 40.7361967,
    "end station longitude": -74.00859207,
    "bikeid": 15334,
    "usertype": "Subscriber",
    "birth year": 1997,
    "gender": "male"
    }
    EOF
    cat << 'EOF' > rides.json
    [
    {
    "tripduration": 602,
    "starttime": "2019-12-01 00:00:05.5640",
    "stoptime": "2019-12-01 00:10:07.8180",
    "start station id": 3382,
    "start station name": "Carroll St & Smith St",
    "start station latitude": 40.680611,
    "start station longitude": -73.99475825,
    "end station id": 3304,
    "end station name": "6 Ave & 9 St",
    "end station latitude": 40.668127,
    "end station longitude": -73.98377641,
    "bikeid": 41932,
    "usertype": "Subscriber",
    "birth year": 1970,
    "gender": "male"
    },
    {
    "tripduration": 1206,
    "starttime": "2019-12-01 00:00:10.9630",
    "stoptime": "2019-12-01 00:20:17.8820",
    "start station id": 362,
    "start station name": "Broadway & W 37 St",
    "start station latitude": 40.75172632,
    "start station longitude": -73.98753523,
    "end station id": 500,
    "end station name": "Broadway & W 51 St",
    "end station latitude": 40.76228826,
    "end station longitude": -73.98336183,
    "bikeid": 18869,
    "usertype": "Customer",
    "birth year": 1999,
    "gender": "male"
    },
    {
    "tripduration": 723,
    "starttime": "2019-12-01 00:00:11.8180",
    "stoptime": "2019-12-01 00:12:14.8310",
    "start station id": 146,
    "start station name": "Hudson St & Reade St",
    "start station latitude": 40.71625008,
    "start station longitude": -74.0091059,
    "end station id": 238,
    "end station name": "Bank St & Washington St",
    "end station latitude": 40.7361967,
    "end station longitude": -74.00859207,
    "bikeid": 15334,
    "usertype": "Subscriber",
    "birth year": 1997,
    "gender": "male"
    }
    ]
    EOF
    cat << 'EOF' > rides_no_header.json
    602,"2019-12-01 00:00:05.5640","2019-12-01 00:10:07.8180",3382,"Carroll St & Smith St",40.680611,-73.99475825,3304,"6 Ave & 9 St",40.668127,-73.98377641,41932,"Subscriber",1970,1
    1206,"2019-12-01 00:00:10.9630","2019-12-01 00:20:17.8820",362,"Broadway & W 37 St",40.75172632,-73.98753523,500,"Broadway & W 51 St",40.76228826,-73.98336183,18869,"Customer",1999,1
    723,"2019-12-01 00:00:11.8180","2019-12-01 00:12:14.8310",146,"Hudson St & Reade St",40.71625008,-74.0091059,238,"Bank St & Washington St",40.7361967,-74.00859207,15334,"Subscriber",1997,1
    EOF
  • Encontre sua connection string do MongoDB .

    Importante

    Sua string de conexão do MongoDB Atlas é semelhante ao exemplo a seguir:

    mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority

mongoimport é uma ferramenta de linha de comando para importar dados de arquivos JSON, CSV e TSV para coleções MongoDB. mongoimport pode ser combinado com outros FERRAMENTAS de linha de comando, como JQ para manipulação de JSON, CSVKit para manipulação de CSV ou curl para baixar dinamicamente arquivos de dados de servidores na Internet.

JSON é um formato de dados hierárquico, como documentos MongoDB , e também é explícito sobre os tipos de dados que codifica.

Os dados CSV (e TSV) são tabulares e cada linha será importada para o MongoDB como um documento separado . Isso significa que esses formatos não podem suportar dados hierárquicos da mesma forma que um documento MongoDB pode. Ao importar dados CSV para o MongoDB, mongoimport tenta fazer escolhas sáveis ao identificar o tipo de campo específico. Esse comportamento pode ser substituído por sinalizadores ou especificação de tipo.

Utilize sua string de conexão do MongoDB Atlas para conectar o mongoimport ao banco de dados do test e coleção do test-collection.

mongoimport --uri 'mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority' \
--db=test \
--collection=test-collection

A conexão bem-sucedida com a collection do seu banco de dados é semelhante ao seguinte:

2025-07-14T10:22:58.317-0400 connected to: mongodb+srv://cluster0.zoikgns.mongodb.net/
2025-07-14T10:23:01.318-0400 test.test-collection 0B
2025-07-14T10:23:04.318-0400 test.test-collection 0B
...

Depois de se conectar com êxito à sua collection de banco de dados, siga os exemplos para saber como usar mongoimport para importar dados para o MongoDB.

Os exemplos a seguir mostram como:

Use a opção --file para especificar a localização e o nome do arquivo que contém os dados a serem importados para uma coleção do MongoDB .

O exemplo a seguir importa o arquivo ride_01.json para a nova collection, test-collection, no banco de dados test fornecendo o caminho do arquivo --file=ride_01.json.

mongoimport --uri 'mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority' \
--db=test \
--collection=test-collection \
--file=ride_01.json

A importação bem-sucedida é semelhante ao seguinte:

2025-07-14T11:47:01.303-0400 connected to: mongodb+srv://[**REDACTED**]@cluster0.zoikgns.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
2025-07-14T11:47:01.391-0400 1 document(s) imported successfully. 0 document(s) failed to import.

Para visualizar a importação de dados bem-sucedida, você pode usar a UI do Atlas.

Importar um documento com dados da UI do mongoimport Atlas
clique para ampliar

Observação

Para cada documento importado, o MongoDB gera um valor _id exclusivo necessário.

Se você não especificar um arquivo, o mongoimport lê dados da entrada padrão (por exemplo stdin). O exemplo a seguir demonstra como importar vários documentos encaminhando-os para mongoimport, que lê os dados de stdin.

echo "{\"bikeid\":\"1234\"}\n{\"bikeid\":\"5678\"}" | mongoimport --uri 'mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority' \
--db=test \
--collection=test-collection-stdin

A importação bem-sucedida é semelhante ao seguinte:

2025-07-14T11:08:08.884-0600 connected to: mongodb+srv://[**REDACTED**]@cluster0.zoikgns.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
2025-07-14T11:08:09.095-0600 2 document(s) imported successfully. 0 document(s) failed to import.

Você pode pipe vários documentos JSON de outra ferramenta, como cat, para importar vários arquivos com mongoimport. No diretório que contém os arquivos JSON para importação, você pode executar o seguinte comando para importar os documentos.

cat *_*.json | mongoimport --uri 'mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority' \
--db=test \
--collection=test-collection-many

A importação bem-sucedida é semelhante ao seguinte:

2025-07-21T16:48:23.519-0400 connected to: mongodb+srv://[**REDACTED**]@cluster0.zoikgns.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
2025-07-21T16:48:23.627-0400 3 document(s) imported successfully. 0 document(s) failed to import.

Para importar um array JSON de documentos, você pode utilizar a opção --jsonArray para especificar o formato dos dados. Você especifica a fonte de dados, que pode ser um arquivo ou uma entrada padrão, separadamente. O exemplo a seguir importa o arquivo rides.json contendo um único array JSON.

mongoimport --uri 'mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority' \
--db=test \
--collection=test-collection-array \
--file=rides.json \
--jsonArray

A importação bem-sucedida é semelhante ao seguinte:

2025-07-14T11:48:10.829-0400 connected to: mongodb+srv://[**REDACTED**]@cluster0.zoikgns.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
2025-07-14T11:48:10.899-0400 3 document(s) imported successfully. 0 document(s) failed to import.

Se você não adicionar a opção --jsonArray, mongoimport falhará com o erro cannot decode array into a Document porque os documentos do MongoDB são comparáveis a objetos JSON, não arrays.

Observação

Você pode armazenar uma array como um valor em um documento, mas um documento não pode ser uma array.

Como o MongoDB armazena dados como BSON, você deve usar o JSON estendido para reconhecer campos importados como JSON como tipos BSON específicos. Isso significa que você pode fornecer um campo que o MongoDB originalmente reconhece como string na estrutura aninhada a seguir para que o MongoDB o reconheça como um tipo date.

"starttime": {
"$date": "2019-12-01T00:00:05.5640Z"
}

Para transformar seus dados existentes, consulte a seção sobre JQ.

Se você tiver arquivos CSV ou TSV para importar, use o --type=csv ou --type=tsv, respectivamente, para informar ao mongoimport o formato esperado.

Se seu arquivo CSV ou TSV tiver uma linha de cabeçalho, use a opção --headerline para informar mongoimport que a primeira linha não deve ser importada como um documento.

Como os dados CSV e TSV não incluem informações de tipo, você pode resolver isso especificando os tipos de campo ao chamar mongoimport. A primeira maneira de fornecer mongoimport com informações de tipo é se você listar os nomes de campo na linha de comando com a opção --fields.

mongoimport --uri 'mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority' \
--db=test \
--collection=test-field-file \
--file=rides_no_header.csv \
--type=csv \
--fields="tripduration","starttime","stoptime","start station id","start station name","start station latitude","start station longitude","end station id","end station name","end station latitude","end station longitude","bikeid","usertype","birth year","gender"

Outra maneira de fornecer mongoimport informações de tipo é se você colocar os nomes dos campo em um arquivo, como field_file.txt, e ponto para ele com a opção --fieldFile.

@"
tripduration
starttime
stoptime
start station id
start station name
start station latitude
start station longitude
end station id
end station name
end station latitude
end station longitude
bikeid
usertype
birth year
gender
"@ | Set-Content -Path field_file.txt
echo 'tripduration
starttime
stoptime
start station id
start station name
start station latitude
start station longitude
end station id
end station name
end station latitude
end station longitude
bikeid
usertype
birth year
gender' > field_file.txt
cat << 'EOF' > field_file.txt
tripduration
starttime
stoptime
start station id
start station name
start station latitude
start station longitude
end station id
end station name
end station latitude
end station longitude
bikeid
usertype
birth year
gender
EOF
mongoimport --uri 'mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority' \
--db=test \
--collection=test-field-file \
--file=rides_no_header.csv \
--type=csv \
--fieldFile=field_file.txt

A importação bem-sucedida das opções --fields e --fieldFile é semelhante à seguinte:

2025-07-14T11:48:10.829-0400 connected to: mongodb+srv://[**REDACTED**]@cluster0.zoikgns.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
2025-07-14T11:48:10.899-0400 3 document(s) imported successfully. 0 document(s) failed to import.

Para visualizar a importação de dados bem-sucedida, você pode usar a UI do Atlas.

Importar um arquivo CSV com tipos de campo
clique para ampliar

Acima, você pode ver que o mongoimport toma decisões automaticamente sobre tipos de dados. Para especificar o tipo de alguns ou todos os seus campos, você pode usar a opção --columnsHaveTypes ou adicionar tipos ao arquivo que você usa com a opção --fieldFile.

@"
tripduration.auto()
starttime.date(2006-01-02 15:04:05)
stoptime.date(2006-01-02 15:04:05)
start station id.auto()
start station name.auto()
start station latitude.auto()
start station longitude.auto()
end station id.auto()
end station name.auto()
end station latitude.auto()
end station longitude.auto()
bikeid.auto()
usertype.auto()
birth year.auto()
gender.auto()
"@ | Set-Content -Path field_file_with_types.txt
echo 'tripduration.auto()
starttime.date(2006-01-02 15:04:05)
stoptime.date(2006-01-02 15:04:05)
start station id.auto()
start station name.auto()
start station latitude.auto()
start station longitude.auto()
end station id.auto()
end station name.auto()
end station latitude.auto()
end station longitude.auto()
bikeid.auto()
usertype.auto()
birth year.auto()
gender.auto()' > field_file_with_types.txt
cat << 'EOF' > field_file_with_types.txt
tripduration.auto()
starttime.date(2006-01-02 15:04:05)
stoptime.date(2006-01-02 15:04:05)
start station id.auto()
start station name.auto()
start station latitude.auto()
start station longitude.auto()
end station id.auto()
end station name.auto()
end station latitude.auto()
end station longitude.auto()
bikeid.auto()
usertype.auto()
birth year.auto()
gender.auto()
EOF
mongoimport --uri 'mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority' \
--db=test \
--collection=test-field-file \
--file=test-datasets/rides_no_header.csv \
--type=csv \
--fieldFile=field_file_with_types.txt

A importação bem-sucedida é semelhante ao seguinte:

2025-07-14T11:48:10.829-0400 connected to: mongodb+srv://[**REDACTED**]@cluster0.zoikgns.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
2025-07-14T11:48:10.899-0400 3 document(s) imported successfully. 0 document(s) failed to import.

Para visualizar a importação de dados bem-sucedida, você pode usar a UI do Atlas.

Importar um arquivo CSV com tipos de campo e tipos de dados
clique para ampliar

Agora, você pode ver que o mongoimport utiliza os tipos de dados que você fornece para armazenar o arquivo importado.

Você pode usar outros programas de linha de comando juntamente com mongoimport para simplificar ainda mais a importação de dados.

JQ é um processador de dados JSON. Ele incorpora uma poderosa linguagem de filtragem e script para filtrar, manipular e gerar dados JSON. Você pode usar um pipe de vários estágios para pipe dados para mongoimport via JQ. Para saber mais sobre como usar o JQ, consulte o Manual do JQ.

CSVKit é uma collection de FERRAMENTAS para filtrar e manipular dados CSV. csvgrep csvcut mongoimport. Para saber mais sobre como usar o CSVKit, consulte a Documentação do CSVKit.

Voltar

Exemplos

Nesta página