知識點
- mongodb簡介
- django如何集成mongodb
- 實際操作mongodb
簡介
參考文檔
window平臺安裝 MongoDB
代碼地址:Spareibs的Github
mongodb簡介
MongoDB是基于文檔(Document)的NoSQL數據庫。文檔是MongoDB中數據的基本單元,非常類似于關系數據庫中的行(比行要復雜)。
- database | database | 數據庫
- table | collection | 數據庫表/集合
- row | document | 數據記錄行/文檔
- column | field | 數據字段/域
- index | index | 索引
- primary key | primary key | 主鍵,MongoDB自動將_id字段設置為主鍵
Django如何集成mongodb
- 最好的選擇是mongoengine
- 安裝mongoengine
- 修改setting.py
- 建立與mongo服務器連接
- 建立Document
Django操作mongdb
- 新增數據
- 查詢數據
- 更新數據
- 刪除數據
實驗步驟
安裝mongoengine
(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django_up\class_06>pip install mongoengine
Collecting mongoengine
Downloading mongoengine-0.11.0.tar.gz (352kB)
100% |████████████████████████████████| 358kB 156kB/s
Collecting pymongo>=2.7.1 (from mongoengine)
Downloading pymongo-3.4.0-cp35-none-win_amd64.whl (270kB)
100% |████████████████████████████████| 276kB 235kB/s
Collecting six (from mongoengine)
Using cached six-1.10.0-py2.py3-none-any.whl
Building wheels for collected packages: mongoengine
Running setup.py bdist_wheel for mongoengine ... done
Stored in directory: C:\Users\Administrator\AppData\Local\pip\Cache\wheels\fc\80\d0\3a48dab37e56de6600b07016fcb0c593afcc8926ad76b777e0
Successfully built mongoengine
Installing collected packages: pymongo, six, mongoengine
Successfully installed mongoengine-0.11.0 pymongo-3.4.0 six-1.10.0
Django設置mongodb【settings.py】
INSTALLED_APPS = [
'mongoengine',
]
MONGODB_DATABASES = {
"default": {
"name": "test",
"host": '127.0.0.1',
"tz_aware": True, # 設置時區
},
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.dummy'
}
}
from mongoengine import connect
connect('test', host='127.0.0.1')
Windows下面安裝MangoDB
將MangoDB加入注冊表,安裝可以查看參考文檔,安裝過程略
C:\Program Files\MongoDB\Server\3.4\bin>mongod.exe --logpath "D:\mongodb\data\mongodb.log" --logappend --dbpath "D:\mongodb\data" --port 27017 --serviceName MongoDB --install
C:\Program Files\MongoDB\Server\3.4\bin>mongo.exe
MongoDB shell version v3.4.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.0
Server has startup warnings:
2016-12-18T18:23:53.361+0800 I CONTROL [initandlisten]
2016-12-18T18:23:53.361+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2016-12-18T18:23:53.361+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2016-12-18T18:23:53.361+0800 I CONTROL [initandlisten]
>
詳細各項配置
modules.py
from mongoengine import *
# Create your models here.
class Poem(Document):
# poem
meta = {
# 數據庫中顯示的名字
'collection': 'poem_data'
}
poem_id = SequenceField(required=True, primary_key=True)
author = StringField()
title = StringField()
# 可以定義查詢集
@queryset_manager
def show_newest(doc_cls, queryset):
# 通過poem_id降序顯示
return queryset.order_by('-poem_id')
views.py
from django.shortcuts import render, HttpResponseRedirect
from polls.models import Poem
# Create your views here.
def home(request):
# 主頁將所有的數據庫數據返回
return render(request, 'home.html', {"show_title": "所有詩詞信息", "poems": Poem.objects.all()})
def add(request):
if request.method == 'POST':
author = request.POST.get('author', "")
poem = Poem(author=author)
poem.save()
title = request.POST.get("title", "")
poem.title = title
# 如果添加數據庫沒有的數據,添加試成功的,但是這個tag是不會被保存的
poem.tag = 'tag'
poem.save()
return HttpResponseRedirect('/')
else:
return render(request, 'add.html')
def search(request):
if request.method == 'POST':
author = request.POST.get('author')
poems = Poem.show_newest(author=author)
# 此處的查詢結果poems是一個list
return render(request, 'home.html', {"show_title": "查詢結果", "poems": poems})
else:
return render(request, 'search.html')
def modify(request):
if request.method == 'POST':
id = request.POST.get('id')
author = request.POST.get('author', "")
title = request.POST.get("title", "")
poems = Poem.objects(poem_id=id)
for poem in poems:
poem.update(author=author, title=title)
return HttpResponseRedirect('/')
else:
return render(request, 'modify.html')
def delete(request):
if request.method == 'POST':
id = request.POST.get('id')
poems = Poem.objects(poem_id=id)
for poem in poems:
poem.delete()
return HttpResponseRedirect('/')
else:
return render(request, 'delete.html')
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^add/', views.add, name='add'),
url(r'^search/', views.search, name='search'),
url(r'^modify/', views.modify, name='modify'),
url(r'^delete/', views.delete, name='delete'),
]