LoginSignup
5
4

More than 5 years have passed since last update.

MongoDB でサンプルデータの REST API を作る(参照のみ、プロトタイプ用)

Last updated at Posted at 2017-04-09

EDIT >> 2018/06/02 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

mongodb 3.6 でこの記事で扱っているビルトインの REST API は廃止されたようです。

もしこの記事の内容が必要なら mongodb 3.4 までを使ってください。

ここまで << EDIT


macOS で brew の場合。

MongoDB の導入

$ brew install mongo

==> Caveats
To have launchd start mongodb now and restart at login:
  brew services start mongodb
Or, if you don't want/need a background service you can just run:
  mongod --config /usr/local/etc/mongod.conf

MongoDB ビルトインの REST API を有効にする。

--rest --httpinterface オプションをつけてバックグラウンドで実行。

$ mongod --config /usr/local/etc/mongod.conf --rest --httpinterface &

サンプルデータの導入

ここの内容です。
https://docs.mongodb.com/getting-started/shell/import-data/

$ curl https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json > primer-dataset.json
$ mongoimport --db test --collection restaurants --drop --file primer-dataset.json

確認

$ curl localhost:28017/test/restaurants/

{
  "offset" : 0,
  "rows": [
    { "_id" : { "$oid" : "58ea357315d762733d09d4c4" }, "address" : { "building" : "1007", "coord" : [ -73.856077, 40.848447 ], "street" : "Morris Park Ave", "zipcode" : "10462" }, "borough" : "Bronx", "cuisine" : "Bakery", "grades" : [ { "date" : { "$date" : "2014-03-03T09:00:00.000+0900" }, "grade" : "A", "score" : 2 }, { "date" : { "$date" : "2013-09-11T09:00:00.000+0900" }, "grade" : "A", "score" : 6 }, { "date" : { "$date" : "2013-01-24T09:00:00.000+0900" }, "grade" : "A", "score" : 10 }, { "date" : { "$date" : "2011-11-23T09:00:00.000+0900" }, "grade" : "A", "score" : 9 }, { "date" : { "$date" : "2011-03-10T09:00:00.000+0900" }, "grade" : "B", "score" : 14 } ], "name" : "Morris Park Bake Shop", "restaurant_id" : "30075445" } ,
...以下略
5
4
1

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
5
4