Python - 如何从MongoDB中获取集合中保存的所有文档

时间:2017-11-17 05:24:16

标签: python mongodb pymongo

我将所有图像保存到MongoDB中。现在我想通过获取集合中的所有图像路径来显示所有图像。 我可以从集合中获取所有文档,但是当我得到'path_name'时,它只会在命令提示符中显示一个结果。

我的MongoDB

{
    "_id": "5a093e83ccd7462a086132da",
    "pics_name": "Pics1",
    "path_name": "static/image/greeting/01.jpg"
}

这是Views.py

import pymongo
from project.Greetings import GreetingsImg

@view_config(route_name='testtest', renderer='templates/testtest.jinja2')
def testtest(request):

    greet = GreetingsImg(request)

    greetall = greet.get_all(all)
    for i in greetall:
        print(i)

    allgreetings = i['path_name']
    print(allgreetings)

return {'allgreetings':allgreetings}

这是Greetings.py

class GreetingsImg(object):

def __init__(self, request):
    self.collection_greetings_img = request.db['greetings_img']

def get_by_name(self, pics_name):
    return self.collection_greetings_img.find({ "pics_name": pics_name})

def get_by_path(self, path_name):
    return self.collection_greetings_img.find({ "path_name": path_name})

def get_all(self,all):
    return self.collection_greetings_img.find()

0 个答案:

没有答案