On the server, I have a text file that needs to be fetched and each line from the file should be inserted as a new document in MongoDB.
For instance, if my text file located at URL "http://example.com/sample.txt" contains:
sample.txt
abcdefg
hijklmn
opqrstu
vwxyz
I aim to insert "abcdefg" as one entry in my collection, and the subsequent lines in a similar manner. The desired structure of my database is:
db.test.find().pretty()
{"_id":sdjcvajsvjhdb36,"name":"abcdefg"},
{"_id":chvbjedfve6f7v,"name":"hijklmn"}
{"_id":tuhgiy837842,"name":"opqrstu"}
{"_id":chw67t8533fif3,"name":"vwxyz"}
My current code retrieves the content of the file, adds "\n" between the lines, and inserts them as a single entry in the database.
var file="http://example.com/sample.txt"
var result=HTTP.call('GET',file, { auth:"test:test"});
console.log(result.content);
productImage.insert({"image":result.content});
The resulting entry in the collection would be:
{ "_id" : "cqcgPTEb62ay8L7Gq", "name" : "abcdefg\nhijklmn\nopqrstu\nuvwxyz" }
This code snippet is implemented within /meteorApp/server/method.js. Is there a way to achieve this goal differently? Can we directly query the MongoDB database and insert in the specified format?