mongodb - 从 csv 导入数据时如何更改 mongodb 中自动生成的 _id 字段?

标签 mongodb csv mongodb-query database

我正在使用 mongoimportcsv 文件导入数据。为此,我执行以下命令

mongoimport -d {databaseName} -c {collectionName}--type csv --file {fileName}  --headerline

它使用 objectId 值创建自动生成 _id 字段。并在其上创建索引。 但我希望它不应该为此创建索引。如何在 _id 和该 csv 文件的字段上创建复合索引?

最佳答案

引用 MongoDB documentation

MongoDB creates the _id index, which is an ascending unique index on the _id field, for all collections when the collection is created. You cannot remove the index on the _id field.

但是您可以创建一个复合索引,其中您的索引可以包含对 _id 字段的引用。

语法是 db.test.ensureIndex({"_id": 1, "name": 1}) 例如,其中 name 是文档中的另一个字段. More info

关于mongodb - 从 csv 导入数据时如何更改 mongodb 中自动生成的 _id 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28735442/

相关文章:

python - 解析多个大型 XML 文件并写入 CSV

给出错误 : Expression $gt takes exactly 2 arguments. 1 的 Mongodb 查询已传入

MongoDB 上的 JSONPath 查询?

使用 $map 进行 MongoDB 嵌套数组搜索

javascript - 无法从 everyauth 获取自定义用户

javascript - × React - fetch ('/' ) 不会命中 Express.router 中的索引路由

mongodb - 如何在 MongoDB 中查找特定类别的最常见值?

c++ - 如何读取 CSV 文件并分配给特征矩阵?

mongodb - 无法将 $match 运算符用于带有 ObjectId 的 mongodb/mongoose 聚合

python - Apache Spark (PySpark) 在读取 CSV 时处理空值