D
D
d22072020-05-16 19:46:25
Node.js
d2207, 2020-05-16 19:46:25

How to write local file to MongoDB?

Hello!
An avalanche of stupid questions is coming at you. It is not possible to throw my File.json into mongobd. To work with mongo I use mongoose, in its documentation I confess I did not understand how to write a local file.
Googling a little, I found this lib , just she knows how to do what I need (example 4), but does not. For example, there are no questions, everything is clear there, from where, what and where will be recorded. To tell the truth, I do not understand why eiba does not work.

const streamToMongoDB = require('stream-to-mongo-db').streamToMongoDB;
const JSONStream      = require('jsonstream');
const fs              = require('fs');

// where the data will end up
const outputDBConfig = {
    dbURL: 'mongodb+srv://d2207:<password>@cluster0-37he4.mongodb.net/test?retryWrites=true&w=majority',
    collection: 'scrapedProducts',
};

// create the writable stream
const writableStream = streamToMongoDB(outputDBConfig, {useNewUrlParser: true, useUnifiedTopology: true});

// create readable stream and consume it
fs.createReadStream('./products.json')
    .pipe(JSONStream.parse('./products.json'))
    .pipe(writableStream);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question