LoginSignup
2
3

More than 5 years have passed since last update.

MongoDBにJSファイルからコレクション作成

Posted at

やりたいこと

コレクションの中身をあらかじめJSファイルに書いてMongoDBに突っ込む

前提

  • CentOS6
  • MongoDB v3.2.0

手順

JSファイル作成

// collection.js
db.collectionname.drop();

// コレクションの中身を変数に
var content = [{
    "foo": "foo1",
    "bar": "bar1"
}, {
    "foo": "foo2",
    "bar": "bar2"
}]

db.collectionname.save(content);
  • MongoDBのsaveメソッドの挙動については以下参照

What is the difference between save and insert in Mongo DB?

MongoDBに突っ込む

mongo # Mongoシェルに入る
use test 
load("absolutepath_to_file/collection.js")

# 成功したら true が出力される
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3