Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过“查询”_id";不';t使用PyMongo在Python的MongoDB中返回文件_Python_Django_Mongodb_Pymongo_Gridfs - Fatal编程技术网

通过“查询”_id";不';t使用PyMongo在Python的MongoDB中返回文件

通过“查询”_id";不';t使用PyMongo在Python的MongoDB中返回文件,python,django,mongodb,pymongo,gridfs,Python,Django,Mongodb,Pymongo,Gridfs,我在Openshift中使用Python+MongoDB+PyMongo import os import gridfs from django.http import HttpResponse from pymongo.connection import Connection from django.shortcuts import get_object_or_404, render_to_response from django.http import HttpResponseRedirec

我在Openshift中使用Python+MongoDB+PyMongo

import os
import gridfs
from django.http import HttpResponse
from pymongo.connection import Connection
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.template import Context, RequestContext,loader

connection = Connection('mongodb://sbose78:XXXXXX@staff.mongohq.com:10068/BOSE')
db=connection['BOSE']
fs=gridfs.GridFS(db)
当我通过_id查询文件时,我得到的就是这个

>>> fs.exists({"_id":'504a36d93324f20944247af2'})
False
当我使用相应的文件名进行查询时:

>>> fs.exists({"filename":'foo.txt'})

True
可能出了什么问题

谢谢

fs.exists({"_id":ObjectId('504a36d93324f20944247af2')})

您需要使用
ObjectId

对于pymongo版本<2.2,您需要使用

from pymongo.objectid import ObjectId
对于版本2.2和更高版本,导入为

from bson.objectid import ObjectId
然后您可以像这样查询gridfs:

fs.exists(ObjectId('504a36d93324f20944247af2'))

我得到了这个名称错误:没有定义名称'ObjectID'。它在这里对我有用。你能在这里显示你所有的代码吗?用imports Vivek编辑了问题描述。我想看看你的对象id是否正确。你能展示获取对象id的代码吗?不,不,让mongo生成objectID。当您通过gridfs将对象放入/写入mongo db时,您将获得一个对象id。使用它。