I'm playing around with learning javascript/node.js for UI and API testing, so I'm busy creating an API that interacts with a mongo DB.
I've written this small js script to populate my db with two data sets.
my script as it stands works perfectly, but I've noticed in the mongo logs I need to close the connection once I'm done, but I'm struggling on finding a good example of how to do this or struggling to use existing examples to fit my script.
#! /usr/bin/node
//Require mongodb drivers
var mongodb = require("mongodb");
//Call test data to populate db
let movieList = require("./data/MovieList");
let actorList = require("./data/ActorList");
//Create MongoObj
var MongoClient = mongodb.MongoClient;
var url = "mongodb://localhost:27017";
const dbName = "MovieDatabase";
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
if (err) {
console.log(`Error - unable to connect to mongodb ${err}`);
process.exit(1);
} else {
console.log(`Connection Successful`);
}
var dbo = db.db(dbName);
if (movieList.getAllMovies !== null) {
createAndPopulateCollections(dbo, 'movies', movieList.getAllMovies)
}
if (actorList.getAllActors !== null){
createAndPopulateCollections(dbo, 'actors', actorList.getAllActors)
}
//db.close();
})
function createAndPopulateCollections(dbo, collectionName, collectionObject) {
dbo.createCollection(collectionName);
dbo
.collection(collectionName)
.insertMany(collectionObject, function(err, result) {
if (err) {
console.log(`ERROR: ${err}`);
process.exit(1);
} else {
console.log(`Success: ${result.insertedCount}`);
process.exit(0);
}
});
}
I'm aware there is probably loads of issues with the above script#!
if you need close your collection you have just write this line in the end of your function
dbo.close();
Related
My question is similar to this post but the solution didnt work for me probably because im using a different type of mysql connection (pool). This is my code:
let config= {
host: '***',
user: 'admin',
password: '***,
port: '3306',
database: '***',
multipleStatements: true
};
const con = mysql.createPool(config);
select();
function select(){
return new Promise((resolve, reject) => {
con.getConnection(function (err, connection) {
if (err) throw err;
else
console.log("Connected!");
let sql = "SELECT * FROM bidPrice WHERE idExchangePlatform = 2;";
connection.query(sql, function (err, results, fields) {
connection.release();
connection.destroy();
if (err) throw err;
console.log(results)
resolve(results);
});
});
});
}
I also important to mention that im running this function using the following command
node --max-old-space-size=31744 index.js # Increase to 31 GB
This is because im working with millions of records from the database query
If i run this with regular node command i would be getting Javascript heap out of memory
When i tried integrating the solution i mentioned earlier to my code i just get a "killed" log after a while and then the process stops, should i handle server disconnect in a different way when using mysql.pool?
If you have big table with many rows, you will must check index for column 'idExchangePlatform' and create if doesn't make it
And simple variant your code:
function select(){
return new Promise((rs, rj) => {
let sql = "SELECT * FROM bidPrice WHERE idExchangePlatform = 2;";
pool.query(sql, (err, rows) => {
if(err)
return rj(err);
return rs(rows);
})
});
}
I'm building a website that lets people write sticky notes and print it to them on the screen. I want to store the sticky notes inside a mongoDB with a db called stickyNotes and a collection called stickyNotes which currently has two documents.
I have a variable called stickyNotes which suppose to get the documents from the stickyNotes collection on the db but when I use the collection.find.toArray from the mongodb library to enter the documents to the stickyNotes variable in an asynchronous way, it shows an empty array value.
This is my server.js file:
const express = require("express");
const mongo = require("mongodb").MongoClient;
const app = express();
let stickyNotes = [];
//mongodb get all sticky notes
const mongoUrl = "mongodb://localhost:27017";
mongo.connect(mongoUrl, { useNewUrlParser: true }, async function(
err,
connection
) {
if (err) {
console.error(err);
} else {
console.log("Succesfully connected to the database");
const db = connection.db("stickyNotes");
const stickyNotesCollection = db.collection("stickyNotes");
stickyNotes = await stickyNotesCollection.find({}).toArray();
}
connection.close();
});
console.log(stickyNotes);
app.use(express.static("./src/public"));
app.get("/sticky-notes", (req, res) => {
console.log("Got a request for sticky notes");
res.json(stickyNotes);
});
const port = 3000;
app.listen(port, () => {
console.log(`App is running on port ${port}`);
});
Can try with:
stickyNotesCollection.find({}, (err, result) => {
if (err) throw err;
stickyNotes = result;
});
or find result in array:
collection.find().toArray(function(err, result) {
console.log(result);
});
or iterate:
collection.find().each(function(err, result) {
//once result
});
I am trying to start the server from my node.js application using npm's oracledb package. But In order to do that I need to connect to the database in PRELIM_AUTH mode with SYSOPER or SYSDBA privileges.
Below is the sample code:
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
oracledb.getConnection(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
},
async function(err, connection) {
if (err) {
console.error(err.message);
return;
}
console.log('Connection was successful!');
await connection.startup();
connection.close(
function(err) {
if (err) {
console.error(err.message);
return;
}
});
});
There's currently (in node-oracledb 2) no support for PRELIM_AUTH mode. Please open an enhancement request on GitHub.
Can i use returning promise in socket.io "socket.on"? Like the following code.
socket.on('news', function (data) {
return News.findOne({id: data.id})
.then((news) => {
socket.emit('event', data.body);
return news;
})
});
In your socket.io server side part of your code you could use the node module 'node-mongodb-native' which in its documentation section shows you how to save something to your mongoDB instance like this:
var MongoClient = require('mongodb').MongoClient, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
// Insert a single document
db.collection('inserts').insertOne({a:1}, function(err, r) {
assert.equal(null, err);
assert.equal(1, r.insertedCount);
// Insert multiple documents
db.collection('inserts').insertMany([{a:2}, {a:3}], function(err, r) {
assert.equal(null, err);
assert.equal(2, r.insertedCount);
db.close();
});
});
});
I'm adding MongoDB to my Express.js Node web app. This is what I got so far:
// in app.js
var mongodb = require('mongodb');
var mongourl = /* … */;
// These are just examples:
app.get('/write', function (req, res) {
mongodb.connect(mongourl, function (err, db) {
db.collection('Users', function (err, coll) {
coll.insert(/* stuff */, function (err) {
res.send(200, 'Done.');
});
});
});
});
app.get('/read', function (req, res) {
mongodb.connect(mongourl, function (err, db) {
db.collection('Users', function (err, coll) {
coll.find({}, function (err, cursor) {
cursor.toArray(function (err, items) {
res.send(200, items);
});
});
});
});
});
Assuming that I want to stick with the default mongodb driver (for now):
Is this pattern right? Do I have to open a new connection to the database in each of my different routes that perform database operations?
If the pattern is right, then how do I deal with the obvious code repetition going on here? Obviously, as it stands now, the code is not acceptable.
Use the new standard, MongoClient. It manages the pool for you, defaults to 5.
//require as a module to be used anywhere.
module.exports = {}
var MongoClient = require('mongodb').MongoClient;
var mongoURI = /* … */;
MongoClient.connect(mongoURI, function(err, db) {
if(err) throw err;
module.exports.users = db.collection('users');
console.log('Connected to Mongo!')
})
then
var db = require('./db.js')
//once connected
//db.users.find()... etc
check out:
http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html
pooling details:
http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html#connection-pool-configuration
Do not close and reopen connection, you're just loosing resources :s