我正在使用 mongoimport
从 csv
文件导入数据。为此,我执行以下命令
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/