你可以使用javascript API在GridFS上使用查找查询吗?

Jos*_*ias 0 mongodb node.js gridfs

据我了解,当你在GridFS中输入内容时,它会被引入2个不同的集合中.一个用于原始数据块,另一个用于元数据文件.

根据我对MongoDB文档的理解,您只能从GridFS中检索带有id或名称的文档.

var gs = new mongodb.GridStore(db, "test.png", "w", {
"content_type": "image/png",
"metadata":{
    "author": "Daniel"
},
"chunk_size": 1024*4
Run Code Online (Sandbox Code Playgroud)

});

那么如果我想从GridFS获取文档的子集怎么办?例如,如果我想要所有GridStore:

元数据:{作者:"丹尼尔"}

为什么我不能使用标准的mongo查询{field:somevalue}并以这种方式检索文档?

有谁知道如何做到这一点?我在node.js上使用javascript API.

rob*_*lep 6

您可以db.files像任何其他集合一样查询集合:

db.collection('fs.files')
  .find({ 'metadata.author' : 'Daniel' })
  .toArray(function(err, files) {
    if (err) throw err;
    files.forEach(function(file) {
      var gs = new mongodb.GridStore(db, file._id, 'r');
      ...
    });
  });
Run Code Online (Sandbox Code Playgroud)

虽然forEach您可能想要使用async.each或任何其他async.*方法,而不是普通的.