0

I'm connecting with mongodb this way:

routes.js:

"use strict"

const Router = require("koa-router")
const bind = require("koa-clean")

const FsFilesController   = require ("./controllers/fsfiles-controller")
    
const fsfiles = new Router({ prefix: "/fsfiles" })
    .post  ("/get",  bind (FsFilesController.get))

const root = new Router({ prefix: "/api" })
    .use  (fsfiles.routes())

module.exports = [ root ]

fsfiles-controller:

"use strict"

const Vars  = require ("../models/fsfiles")

const get = async ({ db }, { filename, sub_key }) => {
    
    const variable = await Vars.get (db, filename)
    console.log("variable: ", variable)

    //if (variable && variable[sub_key])
    //    return variable[sub_key]

    if (variable)
        return variable
    else
        return [ 404, "not found" ]        
}

module.exports = { get }

fsfiles.js:

"use strict";

const __MODULE__ = "fs.files"

const get = (db, filename) =>
    db.collection(__MODULE__).findOne({ filename })

module.exports =
    { get }

My MongoDB collections: https://i.stack.imgur.com/nNoYs.png

I'm requesting the data from a c++ project using curl as:

auto data = Request("https://....herokuapp.com/api/fsfiles/get"
, "POST"
, R"({"filename": "test.txt", "sub_key": ""})");

However, this is what being downloaded to data:

{"_id":"632e4cc00f702f1908c66d83","length":3,"chunkSize":261120,"uploadDate":"2022-09-24T00:18:09.361Z","filename":"test.txt","metadata":{}}

How do i download the content of the test.txt file?

Cesar
  • 41
  • 2
  • 5
  • 16

1 Answers1

0

Use the mongo-c-driver or mongo-cxx-driver library. See the official GridFS C sample or C++ sample.

relent95
  • 3,703
  • 1
  • 14
  • 17
  • I would like to download it from a post request. – Cesar Sep 24 '22 at 05:55
  • You want to implement a web application which serves a content of a file stored in the GridFS? Then, see [this question](https://stackoverflow.com/questions/67047703/how-to-fetch-file-from-mongodb-gridfs-using-graphql). – relent95 Sep 24 '22 at 06:19
  • Do i need to write a function to get each chunk of the file concat and convert from base64? – Cesar Sep 24 '22 at 06:23
  • You need to edit the question or post a new one. Anyway, the base64 encoding in that answer was just an example as the author told at the beginning. If you don't want to implement it yourself, try one such as [gridfs-stream](https://www.npmjs.com/package/gridfs-stream). – relent95 Sep 24 '22 at 06:38
  • what is `response`? in the example on the link you post – Cesar Sep 24 '22 at 07:55