简体   繁体   English

如何在 Windows PowerShell 中使用 mongoimport?

[英]How to use mongoimport in Windows PowerShell?

How to load data to MongoDB using Windows PowerShell?如何使用 Windows PowerShell 将数据加载到 MongoDB?

I have installed MongoDB on computer with Windows.我已经在装有 Windows 的计算机上安装了 MongoDB。 It is in the path: "C:\\Program Files\\MongoDB\\Server\\4.0\\bin\\".它在路径中:“C:\\Program Files\\MongoDB\\Server\\4.0\\bin\\”。 I have created required folder \\data\\db on C drive.我在 C 驱动器上创建了所需的文件夹 \\data\\db。 I have started mongod.exe and it is running.我已经启动了 mongod.exe 并且它正在运行。 I would like to load a CSV file to MongoDB database.我想将 CSV 文件加载到 MongoDB 数据库。

Using .\\mongoimport.exe --help command, we can notice that in PowerShell to load data to MongoDB, we are not using hyphen-minus but colon.使用.\\mongoimport.exe --help命令,我们可以注意到在 PowerShell 中将数据加载到 MongoDB 中,我们使用的不是连字符减号而是冒号。 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.并尝试在存储文件的文件夹中的 PowerShell 中执行它。

Error message is displayed:显示错误信息:

error validating settings: only one positional argument is allowed try 'mongoimport --help' for more information错误验证设置:只允许一个位置参数尝试“mongoimport --help”了解更多信息

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.我不认为在这种情况下使用@params ( @params ) 或常规参数传递 ( $params ) 没有区别。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM