gpt4 book ai didi

mongodb - 从 mongodb shell 运行具有 mongodb 查询的脚本

转载 作者:行者123 更新时间:2023-12-05 08:13:48 28 4
gpt4 key购买 nike

我的 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});
....

关于mongodb - 从 mongodb shell 运行具有 mongodb 查询的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8743385/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com