安装依赖

pip install mongoengine==0.18.2

setting.py 中配置mongodb数据库

  1. # 将默认数据库设置为none
  2. DATABASES = {
  3. # 'default': {
  4. # 'ENGINE': 'django.db.backends.sqlite3',
  5. # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  6. # }
  7. 'default': {
  8. 'ENGINE': None,
  9. }
  10. }
  11. # 连接mongodb数据库
  12. from mongoengine import connect
  13. connect('bdyl', host='127.0.0.1', port=27017)

建立基于mongo数据库的model

  1. from django.db import models
  2. from mongoengine import *
  3. class BaiduData(Document):
  4. search_value = StringField(required=True)
  5. time = DateTimeField(required=True)
  6. location = StringField()
  7. video_top1 = ListField()
  8. audio_top1 = ListField()
  9. video_list = ListField()
  10. audio_list = ListField()
  11. qa_list = ListField()
  12. article_list = ListField()
  13. meta = {
  14. 'collection':'BaiduData'
  15. }

增加数据

  1. @api_view(['GET', 'POST'])
  2. def get_bdyl_index(request):
  3. result = ''
  4. keyword = request.GET.get('keyword','')
  5. result = getData(keyword)
  6. result['time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  7. # 存入 mongoDB
  8. baidudata = BaiduData(
  9. search_value=keyword,
  10. time=result['time'],
  11. video_top1=result['video_top1'],
  12. audio_top1=result['audio_top1'],
  13. video_list= result['video_list'],
  14. audio_list= result['audio_list'],
  15. qa_list=result['qa_list'],
  16. article_list=result['article_list'],
  17. )
  18. baidudata.save()
  19. return HttpResponse(json.dumps(result), content_type="application/json")

删改查

  1. # 查
  2. result = StudentModel.objects.filter(name='水痕')
  3. print(result[0].age)
  4. # 改
  5. result = StudentModel.objects.filter(name='水痕').first().update(name='张三')
  6. print(result)
  7. # 删
  8. result = StudentModel.objects.filter(name='张三').first().delete()
  9. print(result)

pip install mongoengine==0.18.2

setting.py 中配置mongodb数据库

  1. # 将默认数据库设置为none
  2. DATABASES = {
  3. # 'default': {
  4. # 'ENGINE': 'django.db.backends.sqlite3',
  5. # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  6. # }
  7. 'default': {
  8. 'ENGINE': None,
  9. }
  10. }
  11. # 连接mongodb数据库
  12. from mongoengine import connect
  13. connect('bdyl', host='127.0.0.1', port=27017)

建立基于mongo数据库的model

  1. from django.db import models
  2. from mongoengine import *
  3. class BaiduData(Document):
  4. search_value = StringField(required=True)
  5. time = DateTimeField(required=True)
  6. location = StringField()
  7. video_top1 = ListField()
  8. audio_top1 = ListField()
  9. video_list = ListField()
  10. audio_list = ListField()
  11. qa_list = ListField()
  12. article_list = ListField()
  13. meta = {
  14. 'collection':'BaiduData'
  15. }

增加数据

  1. @api_view(['GET', 'POST'])
  2. def get_bdyl_index(request):
  3. result = ''
  4. keyword = request.GET.get('keyword','')
  5. result = getData(keyword)
  6. result['time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  7. # 存入 mongoDB
  8. baidudata = BaiduData(
  9. search_value=keyword,
  10. time=result['time'],
  11. video_top1=result['video_top1'],
  12. audio_top1=result['audio_top1'],
  13. video_list= result['video_list'],
  14. audio_list= result['audio_list'],
  15. qa_list=result['qa_list'],
  16. article_list=result['article_list'],
  17. )
  18. baidudata.save()
  19. return HttpResponse(json.dumps(result), content_type="application/json")

删改查

  1. # 查
  2. result = StudentModel.objects.filter(name='水痕')
  3. print(result[0].age)
  4. # 改
  5. result = StudentModel.objects.filter(name='水痕').first().update(name='张三')
  6. print(result)
  7. # 删
  8. result = StudentModel.objects.filter(name='张三').first().delete()
  9. print(result)