Querying by “_id” doesn't return file in MongoDB in Python, using PyMongo

I'm using Python + MongoDB + PyMongo in Openshift

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)

When I query a file by _id, this is what I get.

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

When I query with the corresponding filename:

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

True

What could be possibly be going wrong?

Thanks.

Answers


For pymongo versions < 2.2, you need to import ObjectId with

from pymongo.objectid import ObjectId

For versions 2.2 and above, the import is instead

from bson.objectid import ObjectId

Then you can query gridfs like so:

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

Need Your Help

Is there a way to essentially “override” a final variable?

java inheritance override

I know you can't override variables, but is there a way to essentially "override" the final worker variable below?

Adding a child widget to another widget in Qt

c++ opengl qt4

Although there are similar questions to mine posted on stackoverflow, none of their solutions actually respond to my problem. I have 2 independent widgets that I would like to combine (insert one w...