Supek
Supek

Reputation: 49

How to use mongoimport in Windows PowerShell?

How to load data to MongoDB using Windows PowerShell?

I have installed MongoDB on computer with Windows. It is in the path: "C:\Program Files\MongoDB\Server\4.0\bin\". I have created required folder \data\db on C drive. I have started mongod.exe and it is running. I would like to load a CSV file to MongoDB database.

Using .\mongoimport.exe --help command, we can notice that in PowerShell to load data to MongoDB, we are not using hyphen-minus but colon. I have created this code:

$params = 'db:', 'db_name',
          'collection:', 'collection_name',
          'type:', 'file_type',
          'file:', 'file_name',
          'headerline'
& "C:\Program Files\MongoDB\Server\4.0\bin\mongoimport.exe" @params

and trying to execute it in PowerShell in folder where file is stored.

Error message is displayed:

error validating settings: only one positional argument is allowed try 'mongoimport --help' for more information

Upvotes: 1

Views: 3653

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200493

You must not omit the hyphens from the parameter names, and I'm not sure the colons are valid.

$params = '--db', 'db_name',
          '--collection', 'collection_name',
          '--type', 'file_type',
          '--file', 'file_name',
          '--headerline'
& 'C:\Program Files\MongoDB\Server\4.0\bin\mongoimport.exe' $params

I don't think it makes a difference whether you use splatting (@params) or regular argument passing ($params) in this case.

Upvotes: 1

lloyd
lloyd

Reputation: 1811

Parameters are included as a single item in the array:

$params = @(
    'db:db_name',
    'collection:collection_name',
    'type:file_type',
    'file:file_name',
    'headerline'
)

&"C:\Program Files\MongoDB\Server\4.0\bin\mongoimport.exe" @params

Upvotes: 0

Related Questions