Uploading a doc file Directly in Mongodb - node.js

I want to save and retrieve a Word doc and PDF file with a size of 1 MB, directly in MongoDB with Node.js. How can I do this is there any article explain about it or can some one help me on this.

Here is the standalone node js code to save the file as binary data in MongoDB. As the maximum file size is 1MB, you can save it in normal collection rather than GridFs.
This can be extended to run as web apps using "express" or "hapi" frameworks. You may need to refer the respective tutorial for that.
Save the file as binary data in MongoDB:-
Note: I have the sample file in "docs" directory. So, I have prefixed it with docs (i.e. "/docs/testplan.docx"). You can remove that if you don't need it.
var MongoClient = require('mongodb').MongoClient;
var Binary = MongoClient.Binary;
var fs = require('fs');
var assert = require('assert');
var url = 'mongodb://localhost:27017/test';
var binaryFileData = fs.readFileSync(__dirname + "/docs/testplan.docx");
var insertDocument = function(db, callback) {
db.collection('file_save').insertOne( {
"fileName" : "testplan.docx",
"fileData" : binaryFileData
}, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the collection.");
callback();
});
};
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
insertDocument(db, function() {
console.log("Closing the database connection...")
db.close();
});
});
Read the file data and save it to disk:-
var MongoClient = require('mongodb').MongoClient;
var Binary = MongoClient.Binary;
var fs = require('fs');
var assert = require('assert');
var url = 'mongodb://localhost:27017/test';
var findDocument = function (fileName, db, callback) {
db.collection('file_save').findOne({"fileName" : fileName }, function (err, document) {
assert.equal(null, err);
console.log(document.fileName);
fs.writeFileSync("testplan_out.docx", document.fileData.buffer);
console.log("File has been written to disk");
callback();
});
};
MongoClient.connect(url, function (err, db) {
assert.equal(null, err);
findDocument("testplan.docx", db, function () {
db.close();
});
});

that works perfectly alright! But I'm trying to upload the document from the POSTMAN and I'm developing my project with MEAN stack.
//document-model.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var documentSchema = {
docFile: {
type: String
}
};
mongoose.model('FileDocument', documentSchema);
//document-route.js
var express = require('express'),
documentRoute = express.Router(),
document = require('../controllers/document-controller');
documentRoute.post('/upload', document.uploadDocument);
module.exports = documentRoute;
//document-controller.js
var document = {
uploadDocument: function (req, res) {
var fileDocument = new FileDocument({
docFile: req.body.docFile
});
fileDocument.save(function (err, result) {
if (err) {
res.status(500).send(err);
} else {
res.status(200).send('Document Uploaded Successfully');
}
});
}
};
I was trying in this way but it is not uploading to mongodb the result is gives in the mongo shell.
{ "_id" : ObjectId("59693872b8b83f42b42a3b9f"), "docFile" : "", "__v" : 0 }

Related

Unable to get insert data in MongoDb using NodeJS

I am creating api using Mongodb and nodeJS as a backend.I have created separate database connection file for that and trying to insert data in other file after importing database file in that.I have connect.js file in db folder and category.js file in routes folder.
But I am getting below error:
TypeError: Cannot read property 'db' of undefined
Below is my code:
connect.js
const MongoClient = require('mongodb').MongoClient;
const dotEnv = require('dotenv').config();
const url = process.env.URI;
const conn = () => {
MongoClient.connect(url,{useNewUrlParser:true,useUnifiedTopology:true})
.then((db) => {
}).catch((err) => {
console.log("error",err);
});
}
module.exports = conn;
category.js
const express = require('express');
const router = express.Router();
const conn = require('../db/connect');
router.get('/',(req,res) => {
data = { name:"Digvijay" };
conn().db("ExpDb").collection("Products").insertOne(data,(err,resp) => {
if(err){
console.log("Error",err);
}
else{
console.log("Success");
}
});
});
module.exports = router;
Someone let me know what I am doing wrong.
I am giving everything in the same file. You can segregate the as per requirement.
const MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/MyDb", function (err, db) {
data = { name:"Digvijay" };
db.collection('Products', function (err, collection) {
collection.insert(data);
db.collection('Products').count(function (err, count) {
if (err) throw err;
console.log('Total Rows: ' + count);
});
});
});
it might help to solve the issue.
https://docs.mongodb.com/manual/reference/method/db.collection.insert/#mongodb-method-db.collection.insert
compatible issue with insertOne:
https://docs.mongodb.com/manual/reference/method/db.collection.insertOne/#mongodb-method-db.collection.insertOne

Node mongodb driver can insert, count, but find

I am migrating an existing app to mongodb,
I have this code mostly found on the node native mongodb driver site.
var fs = require('fs-extra');
var MongoClient = require('mongodb').MongoClient;
const mongoConfig = JSON.parse(fs.readFileSync("./configuration/mongo.json", "utf-8").toString());
const assert = require('assert');
const insertDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('documents');
// Insert some documents
collection.insertMany([
{a : 1}, {a : 2}, {a : 3}
], function(err, result) {
assert.equal(err, null);
assert.equal(3, result.result.n);
assert.equal(3, result.ops.length);
console.log("Inserted 3 documents into the collection");
callback(result);
});
};
const findDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('documents');
// Find some documents
collection.find({}).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
console.log(docs)
callback(docs);
});
};
function initDb() {
var uri = "mongodb://" + mongoConfig.user + ":" + mongoConfig.pass + "#" + mongoConfig.host + ":" + mongoConfig.port;
MongoClient.connect(uri, {auth: {user: mongoConfig.user, password: mongoConfig.pass}, authSource: mongoConfig.dbname}, function (err, client) {
if (err) {
console.error(err);
}
const db = client.db('test');
insertDocuments(db, function() {
findDocuments(db, function() {
client.close();
});
});
}
initDb();
Then i get the following output in the console :
Inserted 3 documents into the collection
Found the following records
[]
Using Robomongo from the same computer to the same server using same credentials, i am able to see and edit the datas.
the insert is working fine, as i am writing this i have a dozen of test documents inserted this way.
But find allways return an empty array.
However, using count instaed of toArray returns the corret value.
I am using :
"mongodb": "^3.0.5",
From my package.json
What am i missing pls ?

AngularJS NodeJS OracleDB application

I want to get the data from oraclde db using nodejs and disply it over angularjs based ui. PFB my code for service.js :
var async = require('async');
var oracledb = require('oracledb');
var dbConfig = require('../utility/dbconfig.js');
var response = require('../utility/response.js');
var bodyParser = require('body-parser');
var express = require('express');
var router = express.Router();
router.use(bodyParser.json());
var dbdata='';
var app = express();
var oracledb = require('oracledb');
var dbConfig = require('../utility/dbconfig.js');
var doconnect = function(cb) {
oracledb.getConnection(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
},
cb);
console.log('Connection was successful!');
};
var dorelease = function(conn) {
conn.close(function (err) {
if (err)
console.error(err.message);
});
console.log('Connection closed successfully!');
};
// Optional Object Output Format
var doquery_object = function (conn, cb) {
conn.execute(
"SELECT d.dc_name,c.cobrand_name,c.cobrand_id,c.IS_CACHERUN_DISABLED,c.is_channel,c.environment,c.COBRAND_STATUS_ID,c.deployment_mode,c.db_name,c.gatherer_group FROM cobrand_master c,dc_master d where d.dc_id = c.dc_id ORDER BY c.display_priority",
{},
{ outFormat: oracledb.OBJECT },
function(err, result)
{
if (err) {
return cb(err, conn);
} else {
console.log("----- cobrand_master details (OBJECT output format) --------");
console.log(result.rows);
return cb(null, conn);
}
});
};
async.waterfall(
[
doconnect,
doquery_object
],
function (err, conn) {
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
if (conn)
dorelease(conn);
});
When I am doing npm start in command prompt, I am getting the data in JSON format.I want to save the output and send it over angular js UI. Since I am new to this, can anyone please help me with simple steps to do it with an example.

close or finish event of writestream not fired gridfs

I am trying to write a file into MongoDB using mongoose and GridFS.
However, gridfs writestream is not firing any of the events - close or finish.
Also, it is not firing even the 'error event'(Just in case if there is any error). My nodejs version is 4.4.5 .
Code is below:
var mongoose = require('mongoose');
var formidable = require('formidable'),
http = require('http'),
util = require('util'),
fs = require('fs-extra');
var Regex = require("regex");
var fs = require('fs');
var path=require('path');
var grid =require("gridfs-stream");
var createRequirement = function (req, res) {
var form = new formidable.IncomingForm({
uploadDir: __dirname + '/upload'
});
form.multiples = true;
form.keepExtensions = true;
files = [],
fields = [];
form.on('field', function (field, value) {
})
form.on('file', function (field, file) {
console.log(file.name);
console.log('File uploaded : ' + file.path);
grid.mongo = mongoose.mongo;
var gfs = grid(db.db);
var writestream = gfs.createWriteStream({
filename: file.name,
mode: 'w'
});
fs.createReadStream(file.path).pipe(writestream);
//Below event is not fired.
writestream.on('finish', function (file) {
Company.findOne({
"users.userName": req.user.userName
}).then(function (data) {
var company = data;
if (!company) {
return res.status(404).send({
'Not Found': 'Company data not found'
});
} else {
Contact.findByIdAndUpdate(
file._id, {
$push: {
"attachments": {
id: file._id
}
}
}, {
safe: true,
upsert: true,
new: true
},
function (err, model) {
console.log(err);
}
);
}
});
})
});
form.parse(req);
return;
};
I could figure out the solution after lot of tries .
It was mongoose connection which was causing the issue.
gridfs-stream expects direct Mongo-DB connection.
I wish they had streamlined things and facilitated usage of Mongoose connection.

MongoDB Node JS Driver find toArray getting the Object Id in different format

I am trying to get all documents from a collection as follows:
var server = "localhost";
var port = 27017;
var dbName = "myNewCreation";
var mongodb = require('mongodb');
var mongoClient = mongodb.MongoClient;
var connString = "mongodb://"+server+":"+port+"/"+dbName;
mongoClient.connect(connString, function(err, db) {
console.dir(err);
if(!err) {
var collectionName = "employee";
//get Documents
db.collection(collectionName).find({}).toArray(function (err, docs) {
console.dir(err);
console.dir(docs);
});
}
else{
console.log("Mongo DB could not be connected");
process.exit(0);
}
});
I am getting the results correctly, but the output of the Object ID is in some machine format as follows:
[ { _id: ObjectID { _bsontype: 'ObjectID', id: 'VX%\u000fÜÏ¢6©ø' },
empName: 'riyke',
Dob: '20-31-2300',
salary: '7000' } ]
How to get the Object ID properly in human readable form?
I found the solution and it is working fine, I am just updating my question with answer:
Use toString or toHexString methods as follows:
var server = "localhost";
var port = 27017;
var dbName = "myNewCreation";
var mongodb = require('mongodb');
var mongoClient = mongodb.MongoClient;
var connString = "mongodb://"+server+":"+port+"/"+dbName;
mongoClient.connect(connString, function(err, db) {
console.dir(err);
if(!err) {
var collectionName = "employee";
//get Documents
db.collection(collectionName).find({}).toArray(function (err, docs) {
console.dir(err);
console.dir(docs);
consolde.dir(docs[0]._id.toString());
consolde.dir(docs[0]._id.toHexString());
});
}
else{
console.log("Mongo DB could not be connected");
process.exit(0);
}
});
you can use valueOf() method , ObjectId.valueOf() it will return hex string

Resources