您可以使用javascript API在GridFS上使用查找查询吗? [英] Can you use find queries on GridFS using javascript API?

查看:132
本文介绍了您可以使用javascript API在GridFS上使用查找查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As I understand it, when you enter something into GridFS it gets entered into 2 different collections under the hood. One for the raw chunks of data and one for the meta data files.

据我从MongoDB文档中了解,您只能从GridFS中检索具有ID或名称的文档.

Also from what I understand from MongoDB's documentation is that you can only retrieve a document from GridFS with an id or name.

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

});

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

So what if I want to get a subset of documents from GridFS? For example what if I want all GridStores with:

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

metadata: {author: "Daniel"}

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

Why can't I use standard mongo queries { field: somevalue } and retrieve documents that way?

有人知道该怎么做吗?我在node.js上使用了javascript API.

Does anybody know how this can be done? I'm using the javascript API on node.js.

推荐答案

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

You can query the db.files collection just like any other collection:

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');
      ...
    });
  });

尽管您可能要使用 async.each forEach 或其他任何async.*方法.

Although instead of plain forEach you may want to use async.each or any of the other async.* methods.

这篇关于您可以使用javascript API在GridFS上使用查找查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆