代码之家  ›  专栏  ›  技术社区  ›  Anonymous Creator

mongodb-gridfs中基于元数据的openDownloadStream

  •  0
  • Anonymous Creator  · 技术社区  · 4 年前

    我使用元数据创建了GridFs bucket,页面作为字段,如下所示。

        const bucket = new GridFSBucket(mydb, {
              "DocumentImageFile",
        });
        fs.createReadStream(file)
          .pipe(
                bucket.openUploadStream(documentId.toString(), {
                  metadata: {
                    page: 1,
                  },
                }),
              )
              .on('finish', () => {
                 fs.unlink(file, () => {});
              });
    

    现在我需要根据页码来阅读。但阅读部分似乎并没有这个过滤器。我们怎么能只读取只有page:1的块作为我在存储期间获取的元数据呢。

    const bucket = new GridFSBucket(projectContext.db, {
          bucketName: 'DocumentImageFile',
        });
        const strDocumentId: string = documentData.documentId.toString();
        const downloadStream = bucket.openDownloadStream(
          new ObjectID(strDocumentId),
        );
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   Anonymous Creator    4 年前

    不确定这是否直接可能。目前,我首先使用元数据查询文档id,然后使用该id提取。因此,使用两步过程,我能够解决它。

    const documentImage = await documentImageFileModel.findOne({
          filename: documentData.documentId,
          'metadata.page': 1,
        });
    
        const documentId = documentImage.id;
    
        const bucket = new GridFSBucket(projectContext.db, {
          bucketName: 'DocumentImageFile',
        });
    
        const downloadStream = bucket.openDownloadStream(new ObjectID(documentId));