![](/img/trans.png)
[英]How to return a array of null elements if array doesn't exist in mongodb using pymongo
[英]Querying by “_id” doesn't return file in MongoDB in Python, using PyMongo
我在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
什么可能出错?
谢谢。
对于pymongo版本<2.2,您需要导入ObjectId
from pymongo.objectid import ObjectId
对于2.2及更高版本,导入是相反的
from bson.objectid import ObjectId
然后您可以像这样查询gridfs:
fs.exists(ObjectId('504a36d93324f20944247af2'))
fs.exists({"_id":ObjectId('504a36d93324f20944247af2')})
您需要使用ObjectId
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.