在mysql中我可以通过运行如下命令来管理我的数据库(主要目标是通过顺序对脚本应用更改来自动构建数据库):
mysql -u root my_database < script_1.sql mysql -u root my_database < script_2.sqlscript_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.sqlWhere 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.jscan 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.
更多推荐
发布评论