Mongoimport将数据类型应用于所有字段的字符串

发布于 2025-02-09 06:41:19 字数 406 浏览 1 评论 0原文

当我们导入CSV文件数据MongoDimport自动选择每个字段的数据类型。

是否可以将数据类型应用于所有字段的字符串? - columnShavetypes 可以帮助定义字段类型。但是我不想定义每个字段并手动键入。

这是我的Schell脚本:

cd <path to direcotry of csv files>
$files = Get-ChildItem .
foreach ($f in $files) {
  if ($f -Like "*.csv") {
   mongoimport -d mydb -c mycollection --type csv --file $f --headerline
 }
}

感谢您的帮助!

When we import CSV file data mongodimport automatically choose data type for each field.

Is it possible to apply the data type as the string for all fields? the --columnsHaveTypes can help to define the field type. But I don't want to define each field and type manually.

Here is my schell script:

cd <path to direcotry of csv files>
$files = Get-ChildItem .
foreach ($f in $files) {
  if ($f -Like "*.csv") {
   mongoimport -d mydb -c mycollection --type csv --file $f --headerline
 }
}

Appreciate your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生戏 2025-02-16 06:41:19

我需要将单个字段设置为字符串,但其余的我希望MongoDB自动定义类型。解决方案是在使用MongoImport之前使用标题线并修改CSV的标题行,这是将一个字段(url_key)作为字符串作为字符串的示例代码,但其余自动定义:

# Add the type row to the CSV file for the url_key field
awk -F, 'NR==1 {for (i=1; i<=NF; i++) $i = ($i == "url_key") ? $i ".string()" : $i ".auto()"} 1' OFS=, "$ORIGINAL_CSV" > "$MODIFIED_CSV"

# MongoDB Import Command with Upsert Mode
mongoimport --uri="$MONGO_URI" \
            --collection=$COLLECTION_NAME \
            --type=csv \
            --file="$MODIFIED_CSV" \
            --headerline \
            --columnsHaveTypes \
            --mode=upsert \
            --upsertFields=sku

I needed to set a single field as a string but the rest I wanted mongodb to automatically define the type. The solution was to use headerline and modify the header row of the CSV before using mongoimport, here is example code for keeping one field (url_key) as a string but the rest auto-defined:

# Add the type row to the CSV file for the url_key field
awk -F, 'NR==1 {for (i=1; i<=NF; i++) $i = ($i == "url_key") ? $i ".string()" : $i ".auto()"} 1' OFS=, "$ORIGINAL_CSV" > "$MODIFIED_CSV"

# MongoDB Import Command with Upsert Mode
mongoimport --uri="$MONGO_URI" \
            --collection=$COLLECTION_NAME \
            --type=csv \
            --file="$MODIFIED_CSV" \
            --headerline \
            --columnsHaveTypes \
            --mode=upsert \
            --upsertFields=sku
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文