-
Activate MongoDB server:
$ cd C:\Program Files\MongoDB\Server\3.6\bin $ mongod
Open new terminal & type again:
$ cd C:\Program Files\MongoDB\Server\3.6\bin $ mongo
[OPTIONAL] You can create a database & collection first, or let our Flask application do this later. If you wanna create db & col first, do these on Mongo:
> use lin_flask > db.createUser({user:'lintang', pwd:'1234', roles:['readWrite', 'dbAdmin']}) > db.createCollection('users')
- Clone this repo. Insert your database URI to database.yaml file, then install all the packages needed. In this project I'm using flask, flask_cors & pymongo:
$ git clone https://github.com/LintangWisesa/CRUD_Flask_MongoDB.git $ cd CRUD_Flask_MongoDB $ pip install flask flask_cors pymongo
- Run the server file. Make sure your MongoDB server is still running. Your application server will run locally at http://localhost:5000/ :
$ python app.py
-
Give a request to the server. You can use Postman app:
See the opening screen (home.html)
GET /
Post a data to database:
POST /data body request: {name:"x", age:"y"}
Get all data & specific data by MongoDB ObjectId:
GET /data GET /data/{:id}
Update a data by MongoDB ObjectId:
PUT /data/{:id} body request: {name:"x", age:"y"}
Delete a data by MongoDB ObjectId:
DELETE /data/{:id}
- Enjoy your code! 😎👌
Lintang Wisesa 💌 lintangwisesa@ymail.com
Facebook | Twitter | Google+ | Youtube | :octocat: GitHub | Hackster