I am trying to perform bulk upload in node js and mongodb is my db,can anyone suggest me some best articles regarding this.Thanks in advance.
You can use Model.collection.insert or Model.insertMany as below where collections is array of items in bulk.
Model.collection.insert(collections, function (err, models) {
next(err, models);
});
OR,
Model.insertMany(collections, function (err, models) {
next(err, models);
});
Mongoose reference: http://mongoosejs.com/docs/api.html#model_Model.insertMany
Mongo reference: https://docs.mongodb.com/v3.2/reference/method/db.collection.insert/
You can insert multiple records with batch/bulk insert in mongoose.
var arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];
Movies.insertMany(arr, function(error, docs) {});
Let's say i have an excel file employees.xlsx with following data and i want perform bulk write.
There are several libraries out there for converting excel data to json in node, i use xlsx but it's personal taste you can use whatever is convenient for you.
Here is a helper i use for reading "/public/employees.xlsx" file.I found the content from here here.
//** helper/excel-reader.js **//
var excelReader = {};
excelReader.readExcel = function(filePath){
var XLSX = require('xlsx');
var workbook = XLSX.readFile(filePath);
var sheet_name_list = workbook.SheetNames;
var data = [];
sheet_name_list.forEach(function(y) {
var worksheet = workbook.Sheets[y];
var headers = {};
for(z in worksheet) {
if(z[0] === '!') continue;
//parse out the column, row, and value
var tt = 0;
for (var i = 0; i < z.length; i++) {
if (!isNaN(z[i])) {
tt = i;
break;
}
};
var col = z.substring(0,tt);
var row = parseInt(z.substring(tt));
var value = worksheet[z].v;
//store header names
if(row == 1 && value) {
headers[col] = value;
continue;
}
if(!data[row]) data[row]={};
data[row][headers[col]] = value;
}
//drop those first two rows which are empty
data.shift();
data.shift();
});
return data;
}
module.exports = excelReader;
Now the employee model somehow looks like this.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var employee = new Schema({
name: String,
adderess: String,
phonenumber: String
});
module.exports = mongoose.model('Employee', employee);
Now let's use the above code so here is my users.js route whenever i type "localhost:3000/users" it write the csv content to database.
var express = require('express');
var router = express.Router();
var excelReader = require('../helpers/excel-reader');
var mongoose = require('mongoose');
var Employee = require('../models/employee');
/* GET users listing. */
router.get('/', function(req, res, next) {
var employeesJsonArray = excelReader.readExcel('./public/employees.xlsx')
Employee.insertMany(employeesJsonArray,function(error, docs) {
if(error){
next(error)
}
else{
res.json(docs);//just rendering the document i got
}
});
});
module.exports = router;
Hope this helps!!
Related
Well I want to read json file in my node.js code to update the XML tags in the given XML, suppose I have a file named as object.json, now I want to Get its data, data in object.json is
{
"input": {
"customClass": "input",
"customModule": "abcInput",
"customModuleProvider": "Whatever"
},
"button": {
"customClass": "ViewController",
"customModule": "Testing",
"customModuleProvider": "target"
}
}
I have tried to print data but I was not able to use that data to set attributes, I tried to set attributes for hard coded tags, code for that is
var DOMParser = new (require('xmldom')).DOMParser({ normalizeTags: { default: false } });
var express = require("express"),
bodyParser = require('body-parser');
require("body-parser-xml")(bodyParser);
var xml2js = require('xml2js');
var builder = new xml2js.Builder({ standalone: { default: false } });
var app = express();
//Options of body-parser-xml module
app.use(bodyParser.xml({
xmlParseOptions: {
normalize: false, // Trim whitespace inside text nodes
normalizeTags: false, // Transform tags to lowercase
explicitArray: false // Only put nodes in array if >1
}
}));
//Post Method
app.post('/users', function (req, res, body) {
//Parsing Request.Body
var document = DOMParser.parseFromString(
builder.buildObject(req.body).toString()
);
//Getting a list of elements whose name is being given
var node = document.getElementsByTagName("TextView");
//Changing Tag Name of Specific Elements
for (var i = 0; i < node.length; i++) {
node[i].tagName = "com.mycompany.projectname.TextView";
}
//Getting a list of elements whose name is being given
var node = document.getElementsByTagName("com.example.usmanchattha.custom.TextView");
//Setting attributes
for (var i = 0; i < node.length; i++) {
node[i].setAttribute("android:id", "#+id / text2");
node[i].setAttribute("android:text", "Custom Android Font");
node[i].setAttribute("customfontdemo:chattha", "faizan");
node[i].setAttribute("android:padding", "12dp");
node[i].setAttribute("customfontdemo:fontName", "pipe_dream.ttf");
node[i].setAttribute("android:textSize", "32sp");
}
//Responsing Updated Data
res.send(document.toString());
});
app.listen(1000);
Well I ran into same problem, and then after a hard work I made a solution for this problem, here is the code that I created, it works perfectly fine, if you have any query, please make sure to ask.
var express = require("express"),
bodyParser = require('body-parser');
var DOMParser = new (require('xmldom')).DOMParser({ normalizeTags: { default: false } });
var arrayOfControls = require('./object.json'); //to read object.json file
var app = express();
app.use(bodyParser.text({ type: '*/*' }));
app.post('/users', function (req, res, body) {
var document = DOMParser.parseFromString(
req.body
);
var ControlNames = Object.keys(arrayOfControls); //Returns Control Names
for (var singleControl = 0; singleControl < ControlNames.length; singleControl++) {
var wholeObject = arrayOfControls[ControlNames[singleControl]]; //Returns Property names of index[i] control as an object
var PropertyNames = Object.keys(wholeObject); //returns properties of first control
var listOfElements = document.getElementsByTagName(ControlNames[singleControl]);
if (listOfElements.length < 0) {
continue;
} else {
for (var singleElement = 0; singleElement < listOfElements.length; singleElement++) {
for (var singleProperty = 0; singleProperty < PropertyNames.length; singleProperty++) {
listOfElements[singleElement].setAttribute(PropertyNames[singleProperty], wholeObject[PropertyNames[singleProperty]].toString());
}
}
}
}
req.body = document.toString();
res.send(document.toString());
});
app.listen(1000);
I want to render a page getting info for two different queries in CosmoDB using documentdb.
I have 2 queries:
var FirstQuery = {
query: 'SELECT * FROM FactoryData',
};
var SecondQuery = {
query: 'SELECT * FROM StoreData',
};
And have this to get the data
docDbClient.queryDocuments(collLink, FirstQuery ).toArray(function (err, results) {
value1 = results;
});
docDbClient.queryDocuments(collLink, SecondQuery ).toArray(function (err, results) {
value2 = results;
});
then i want to render the view with those results but i cant get it rendering from outise of this funcions.
res.render('view.html', {"value1" : value1 , "value2" : value2});
I know that this code will not work, but i was trying to implement promises and didn't know how to do it with documentdb-q-promises.
I already read a lot of documentation about Q promise but i dont get it.
Can someone explain to me how i can do it , I`m a beginner.
Based on your requirements,I followed the npm doc and test code on github to test following code in my local express project. Please refer to it.
var express = require('express');
var router = express.Router();
var DocumentClient = require('documentdb-q-promises').DocumentClientWrapper;
var host = 'https://***.documents.azure.com:443/'; // Add your endpoint
var masterKey = '***'; // Add the massterkey of the endpoint
var client = new DocumentClient(host, {masterKey: masterKey});
var collLink1 = 'dbs/db/colls/import';
var FirstQuery = 'select c.id,c.name from c';
var collLink2 = 'dbs/db/colls/item';
var returnArray = [];
client.queryDocuments(collLink1, FirstQuery).toArrayAsync().
then(function(response){
console.log(response.feed);
var map = {};
map['value1'] = response.feed;
returnArray.push(map);
return client.queryDocuments(collLink2, FirstQuery).toArrayAsync()
})
.then(function(response) {
console.log(response.feed);
var map = {};
map['value2'] = response.feed;
returnArray.push(map);
})
.fail(function(error) {
console.log("An error occured", error);
});
router.get('/', function(req, res, next) {
res.send(returnArray);
});
module.exports = router;
Test Result:
Hope it helps you.
I'm learning to use the mean stack and trying to build a url shortener. I've got a module that takes the req.params.UserUrl checks and makes sure it's a valid url then creates a random number that I want to use as the short route. I can't seem to find a way to save the random number so that I can check their next url request against it. After a google search it seemed maybe the most effecient way would be to save an object in the database with the long_url and the short_url:randomNumber. My code doesn't throw any errors but when I check my heroku database it has a new entry but only has the _id and __v that mLabs generates itself. Can someone tell me where I'm going wrong.
Route File
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var URLShortener = require(process.cwd()+'/public/Modules/urlShortener.module.js');
var ShortURL = require('../models/shortUrl.js');
router.get('/', function(req, res) {
res.render('index', { title: 'FreeCodeCamp Projects' });
});
router.get('/urlShortener', function(req, res){
res.render('freecodecamp/urlShortener', { title: 'Url Shortener Site'});
});
router.get('/urlShortener/:userUrl', function(req, res){
if(URLShortener.checkValidUrl(req.params.userUrl))
{
var UserUrl = req.params.userUrl;
var randNbr = URLShortener.assignRanNbr();
ShortURL.create(URLShortener.createUrlObj(UserUrl, randNbr), function (err, smallUrl) {
if (err) return console.log(err);
else res.json(smallUrl);
});
}
else
{
res.send('Invalid url');
}
});
router.get('/:short', function(req, res){
if(randNbr == req.params.short)
{
res.redirect(userUrl);
}
else
{
res.send('Not the correct shortcut');
}
});
module.exports = router;
Url Schema
var mongoose = require('mongoose')
var Schema = mongoose.Schema
var shortUrlSchema = new Schema({
long_id:String,
short_id:Number
}, {collection: 'shortUrl'});
module.exports = mongoose.model('shortUrl', shortUrlSchema);
urlShortener Module
'use strict'
module.exports.checkValidUrl = function(url){
var pattern = new RegExp(/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+#)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+#)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%#.\w_]*)#?(?:[\w]*))?)/);
return pattern.test(url);
}
module.exports.assignRanNbr = function(){
var randNbr = Math.floor(Math.random() * (9999 - 1 + 1)) + 1;
return randNbr;
}
module.exports.createUrlObj = function(url, num){
var urlObj = {};
urlObj.original_url = url;
urlObj.short_url = 'https://rawlejuglal-me-rawlejuglal-1.c9users.io/freecodecamp/'+num;
return urlObj;
}
Your createUrlObj method is returning an object with the properties original_url and short_url, but your shortUrlSchema properties are long_id and short_id. The property names in your create method need to match your schema. The property value types must also match your schema types (currently short_url is a string and short_id is a number). I think what you really want is for your createUrlObj method to be
module.exports.createUrlObj = function(url, num){
var urlObj = {};
urlObj.long_url = url;
urlObj.short_id = num;
return urlObj;
}
and your schema to be
var shortUrlSchema = new mongoose.Schema({
long_url: String,
short_id: Number
}, {collection: 'shortUrl'});
Additionally, your '/:short' route should have a call to the database since the randNbr and userUrl variables are not defined in that route.
router.get('/:short', function(req, res){
ShortUrl.findOne({short_id: req.params.short}, function(err, shortUrl){
if(err) res.send('Invalid Url');
res.redirect(shortUrl.long_url)
})
});
My system will create around 30 collection for every hour. My server get thousands request for one hour. I have big data and multiple collections. And i use MongoDB-NodeJS for saving data.
The modelControl function of pariteModel class at the paritemodel.js -as the below codes- i do check if this schema is created before. And i will create schema or use created schema.
First collections creating and saving data to MongoDB. But when create another collections it's not doing. For example:
EURJPY_20160107_16 collection is created but EURJPY_20160107_17 collection is not create. I have check mongoose.models at modelparite.js EURJPY_20160107_17 is created but instance created from EURJPY_20160107_17 Schema not saved to database.
My server files like this:
app.js file is my bootstrap file:
var http = require('http'),
dispatcher = require('httpdispatcher');
require('./mongo.js');
function handleRequest(request, response){
try {
dispatcher.dispatch(request, response);
} catch(err) {
console.log(err);
}
}
var server = http.createServer(handleRequest);
server.listen(3663, function(){
console.log('listening the port: ' + 3663);
});
This is mongo.js which i call in app.js. This file uses for save data to mongodb:
var dispatcher = require('httpdispatcher'),
url = require('url'),
moment = require('moment'),
md = moment().format("YYYYMMDD_HH"),
myModel = require('./modelparite.js');
dispatcher.onGet('/mongo', function(req, res){
var url_parts = url.parse(req.url, true);
// This is collection name. Output like this: USDTRY_20160107_16
var colName = url_parts.query.symbol + '_' + md;
var tarih = new Date();
var pariteModel = myModel.returnModel(colName);
var yeniParite = new pariteModel({
symbol: url_parts.query.symbol,
bid: url_parts.query.bid,
ask: url_parts.query.ask,
timeup: moment(tarih).format("YYYY-MM-DD HH:mm:ss")
});
yeniParite.save(function (err, data) {
if (err) console.log(err);
console.dir(data,true);
});
res.writeHead(200, {'Content-Type': 'text/html'});
res.end();
});
And this is my model modelparite.js file which i call in mongo.js. This file uses to create schema with Mongoose:
var mongoose = require('mongoose'),
helper = require('./helper.js');
require('mongoose-double')(mongoose);
mongoose.connect('mongodb://localhost:27017/forex');
var pariteModel = {
pariteSchema: "",
initSchema: function(){
var Schema = mongoose.Schema;
this.pariteSchema = new Schema({
symbol: String,
bid: mongoose.Schema.Types.Double,
ask: mongoose.Schema.Types.Double,
timeup: Date
});
},
modelControl: function(modelName){
if(mongoose.models[modelName]){
return true;
}
return false;
},
returnModel: function(modelName){
modelName = helper.whichParity(modelName);
//console.log(modelName);
if(this.modelControl(modelName)){
//console.log(mongoose.model(modelName));
return mongoose.model(modelName);
}else{
this.initSchema();
return mongoose.model(modelName, this.pariteSchema);
}
},
}
module.exports = pariteModel;
Hey so I am trying to use gridfs to store profile pictures... I am not sure what I am doing wrong, I am kind of new to this as well.
This is what I got so far, but it seems to be erroring this:
TypeError: Cannot read property 'primary' of undefined
at Stream.GridStore.open (..\node_modules\mongodb\lib\mongodb\gridfs\gridstore.js:146:69)
This is my code:
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = mongoose.Schema.Types.ObjectId;
var GridStore = require('mongodb').GridStore;
var Step = require('step');
exports.newProfilePicturePost = function(req, res) {
var db = mongoose.createConnection('mongodb://localhost/test5');
var fileId = new ObjectId();
var gridStore = new GridStore(db, fileId, "w", {
"metadata": {
category: 'image'
}, content_type: 'image'});
gridStore.chunkSize = 1024 * 256;
gridStore.open(function(err, gridStore) {
Step(
function writeData() {
var group = this.group();
for(var i = 0; i < 1000000; i += 5000) {
gridStore.write(new Buffer(5000), group());
}
},
function doneWithWrite() {
gridStore.close(function(err, result) {
console.log("File has been written to GridFS");
});
}
)
});
};
Any help or fixes I should make to my code is welcomed, also I am not sure how to specify the collection I want to store the picture in and I want to also add the userId to the picture being saved in the collection for fetching it later.