通过命令行脚本进行MongoDB管理(MongoDB Administration through command line scripts)

编程入门 行业动态 更新时间:2024-10-26 02:32:37
通过命令行脚本进行MongoDB管理(MongoDB Administration through command line scripts)

在mysql中我可以通过运行如下命令来管理我的数据库(主要目标是通过顺序对脚本应用更改来自动构建数据库):

mysql -u root my_database < script_1.sql mysql -u root my_database < script_2.sql

script_1.sql看起来像这样:

DROP DATABASE IF EXISTS my_database; CRATE DATABASE my_database;

和script_2.sql这样的东西:

INSERT INTO ...

好的,我想和mongo一样,但是我想运行mongo db命令而不是运行SQL命令。

有可能吗?

in mysql I can administer my database (the primary goal is to a have an automated build of the database by sequentially applying changes to it with scripts) by running commands like this:

mysql -u root my_database < script_1.sql mysql -u root my_database < script_2.sql

Where script_1.sql looks like this:

DROP DATABASE IF EXISTS my_database; CRATE DATABASE my_database;

And script_2.sql something like this:

INSERT INTO ...

Ok, I want to to the same for mongo, but instead of running SQL commands I would like to run mongo db commands.

Is something like that possible?

最满意答案

首先,在mongo中,即时创建对象(当您将第一个对象插入数据库的第一个集合中时),因此您可能不需要创建数据库和集合。

然后,您可以使用2种不同的工具使用对象填充数据库:mongoimport和mongo

mongo -d myDatabase --eval "db.aCollection.insert({_id : ..,....,....})"

要么

mongo -d myDatabase --eval bob.js

可以执行你想要的任何JavaScript。 这是通过锁定db来完成的,因此不要在用于请求的实例上使用它。

第二种方法是使用mongoimport作为其他响应的说法。 如果您有一个对象列表而没有其他类型的命令,这将更容易。 请注意,对象是upserted(未插入),因此重复项不应失败。

First, in mongo, objects are created on the fly (when you insert the first object in the first collection of a database), so maybe you don't need to create database and collection.

Then you can use 2 different tools to populate your databases with objects : mongoimport and mongo

mongo -d myDatabase --eval "db.aCollection.insert({_id : ..,....,....})"

or

mongo -d myDatabase --eval bob.js

can execute any javascript you want. This is done with a lock on the db, so do not use this on an instance that is used for requests.

The second method is to use mongoimport as said on other response. This is easier if you have a list of objects and no other type of commands. Note that objects are upserted (not inserted) so duplicates should not fail.

更多推荐

本文发布于:2023-07-18 19:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1164485.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令行   脚本   MongoDB   Administration   line

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!