我的 mongodb 数据库文档中有几个 ID。我需要通过脚本在这些 ID 上创建索引,这样我就不必一次又一次地运行 ensureIndex 命令。
db.getCollection("elements").ensureIndex({customerId:1});
db.getCollection("elements").ensureIndex({userId:1});
........
我只是运行脚本(包含所有这些命令),我应该完成了。知道如何从 mongodb shell 运行脚本吗?另外脚本的扩展名应该是什么?
朱希。
最佳答案
您可以将命令放入一个 javascript 文件(扩展名为 .js
)并使用 mongo
控制台实用程序来运行它。像这样:
mongo --host my_host --port my_port my_db create_indexes.js
create_indexes.js
文件将包含您的命令:
db.getCollection("elements").ensureIndex({customerId:1});
db.getCollection("elements").ensureIndex({userId:1});
....
https://stackoverflow.com/questions/8743385/