Search code examples
pythonmongodbprojectiongridfs

Mongo GridFS find with projection issue


I'm trying to do a query with projection in a GridFS like this:

Files = gridfs.GridFS(db)
f = Files.find({'metadata.AgentId': '1234'}, {'_id':1})

And i'm getting this error:

TypeError: skip must be an instance of int

So, the initializer for Cursor is taking {'_id':1} as the third parameter >> skip.

On the other hand, this query is working fine in the Robo3T shell:

enter image description here

Any idea?


Solution

  • There is no projection parameter in Gridfs.find method.

    https://api.mongodb.com/python/current/api/gridfs/index.html#gridfs.GridFS.find https://github.com/mongodb/mongo-python-driver/blob/master/gridfs/grid_file.py#L796-L802

    Maybe you should use the collection's find. https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find

    db['fs.files'].find({'metadata.AgentId': '1234'}, {'_id':1})