使用MongoDB创建样本数据的REST API(仅限参考,用于原型)

编辑>> 2018/06/02 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

据本文报道,MongoDB 3.6 版本似乎已经弃用了所提及的内置 REST API。

如果您需要此文章的内容,请使用MongoDB 3.4版本。

到目前为止


在 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" } ,
...以下略