aheckmann / gridfs-stream

Easily stream files to and from MongoDB
MIT License
615 stars 120 forks source link

find metadata by _id returns null #28

Closed nikmartin closed 10 years ago

nikmartin commented 10 years ago

Trying to query files collection for metadata, using an _id that exists:

db.fs.files.findOne({_id:ObjectId("52b0e044b10cf7546b000010")});
{
        "_id" : ObjectId("52b0e044b10cf7546b000010"),
        "aliases" : null,
        "chunkSize" : 262144,
        "contentType" : "application/octet-stream",
        "filename" : "1.2013-12-0065.Patient.0.1387323432127.jpg",
        "length" : 58978,
        "md5" : "9bc7024ed0c74724d8ad0a8154df40ea",
        "metadata" : {
                "field_name" : "1.2013-12-0065.Patient.0.1387323432127.jpg",
                "run_id" : "52ad3d9bce79b0db0d000020",
                "section" : "Patient"
        },
        "uploadDate" : ISODate("2013-12-17T23:37:40.234Z")
}

using this query:
gfs.files.find({ _id: ObjectId(fileID) }, function (err, file) {
         return file || err;
      });

Both err and file are null. Can the fs.files collection be queried by _id?

Nainterceptor commented 10 years ago

Hi,

Same issue here, but I've found a solution. (I use mongoose)

grid.files.findOne({ "_id" : "52f0af98e47dd6300334d009"}, function (err, file) {
            console.log(err); //Nothing
        console.log(file); //Nothing
    });

var ObjectID = mongoose.mongo.BSONPure.ObjectID; //First, get ObjectID
grid.files.findOne({ "_id" : ObjectID("52f0af98e47dd6300334d009")}, function (err, file) {
            console.log(err); //Nothing
        console.log(file); //My file
    });

Have a nice day