NodeJS/MongoDB - Clone Collection with Indexs - javascript

I'm trying to clone a mongodb collection with the indexs from two different MongoDB instances. After googling around, I found that cloneCollection would be my best bet. I've tried to make my code nice and simple, but I never seem to get the actual clone command to run. 'Started' and 'Authed' pop up in my console, but the command never runs. What is it I'm doing wrong? Thanks.
mongodb = require("mongodb")
#####################
# MONGODB CONFIG
#####################
console.log "Started"
Server = mongodb.Server
Db = mongodb.Db
server = new Server("example.com", 27017, {})
db = new Db("databaseTwo", server,
safe: true
)
db.open (err, db) ->
db.authenticate "user", "password", (err, res) ->
throw err if err
console.log "Authed"
db.command
cloneCollection: "databaseOne.helloThereCollection"
from: "example.com:6001"
key:
username: 'user'
password: 'password'
, (err, cb) ->
console.log err if err
console.log 'Command Ran'
cb null

Related

Problem with Reading a specific table from SQL Server by mssql in javascript ('dbo.index')

I have to read a table named dbo.Index from SQL Server in my node js express app but it fails and returns nothing.
Here's my code:
app.get('/getData', (req, res, next) => {
const config = {
user: 'sa',
password: '12345',
server: 'localhost',
database: 'myDB'
}
const pool = new sql.ConnectionPool(config)
pool.connect(err => {
if (err) console.log(err)
console.log('connected.')
const request = new sql.Request(pool)
request.query(`SELECT * FROM dbo.Index`, (error, recordSet) => {
if (err) console.log(error)
res.send(recordSet)
})
})
})
I've tested this code and it works well with other tables but with this specific name dbo.Index, it fails.
It's necessary for me to read it and I can't change the table name (have no permission).
I use node-mssql package in order to connect to the database.
INDEX is a Reserved word, so you will need to wrap it in square parentheses, like so:
SELECT * FROM [dbo].[Index]
It's generally best to try and avoid using reserved words for table or column names as it easily leads to confusion, mistakes and bugs.

Why does the order of my gets change how the program works?

I encountered a weird bug when doing a quick coding assignment.
Here is my code.
Lets call this 'A'
//Grab all the animals from the database
WebApp.get('/all',(req,res) =>
{
const connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '1234', //Enter your password here
// I found that mySQL 8.0 uses a new default authent plugin whereas 5.7 uses a different one, If you get a ER_NOT_SUPPORTED_AUTH_MODE error from the response, try referring to this post to alter your root password. (https://stackoverflow.com/questions/50373427/node-js-cant-authenticate-to-mysql-8-0)
database: 'animals'
});
const query = "SELECT * FROM animals";
connection.query(query, (err, rows, fields) =>
{
if (err)
{
console.error('error : ' + err.stack);
res.sendStatus(500);
return;
}
console.log("Fetched animals successfully");
//console.log(rows); // Use this for error checking to see if a authent problem occurs.
res.json(rows);
});
});
and this 'B'
//Grab a selected animal from the database given a valid Id.
WebApp.get('/:id',(req,res) =>
{
console.log("Fetching user with id: " + req.params.id);
const connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '1234', //Enter your password here
// I found that mySQL 8.0 uses a new default authent plugin whereas 5.7 uses a different one, If you get a ER_NOT_SUPPORTED_AUTH_MODE error from the response, try referring to this post to alter your root password. (https://stackoverflow.com/questions/50373427/node-js-cant-authenticate-to-mysql-8-0)
database: 'animals'
});
const animalId = req.params.id;
const query = "SELECT * FROM animals WHERE id = ?";
connection.query(query, [animalId], (err, rows, fields) =>
{
if (err)
{
console.error('error : ' + err.stack);
res.sendStatus(500);
return;
}
console.log("Fetched animals successfully");
//console.log(rows); // Use this for error checking to see if a authent problem occurs.
res.json(rows);
});
});
For some reason, if I put A before B it works, and I get successful results from my queries. However, if I put B before A, B will return successfully, but A will return '[]'. Anyone know why?
Thanks for any help!
Have you tried terminating the connection after each request, or considered using a connection pool? I am not familiar with nodeJS integration with MySQL, but in SQLServer, it is best to use ConnectionPool, when asynchronously making database requests.

acess function in objected, nested inside another function

Im trying to manage a connection instance, using a function to handle idle connection disconnect issues, using mysql database and node.js
At moment, i've got following code (coffescript):
mysql = require 'mysql'
handleDisconnect = () ->
connection = mysql.createConnection
host: 'localhost'
user: 'root'
password: 'passroot'
database: 'mydb'
connection.connect (err) ->
if err
console.log 'Error connecting to db: ', err
setTimeout handleDisconnect, 2000
connection.on 'error', (err) ->
console.log 'db error', err
if err.code == 'PROTOCOL_CONNECTION_LOST'
handleDisconnect()
else
throw err
handleDisconnect.instance = connection
module.exports = handleDisconnect
and
express = require 'express'
router = express.Router()
connection = require('../database')().instance
bcrypt = require 'bcryptjs'
router.post '/', (req, res) ->
credential = connection.escape req.body.credential
password = connection.escape req.body.password
res.send credential+password
module.exports = router
Problem is, when i try to access the route, i get following error:
Cannot read property 'escape' of undefined
What am i doing wrong?
I believe your issue is that the final line of handleDisconnect is returning the instance, so you're trying to get the instance from instance, not from handleDisconnect. So you'll need the function to return itself at the end if you want to access properties on it.
You also want the function to be using the equivalent of "this" (# in coffeescript) rather than specifically referring to handleDisconnect.
Example code:
mysql = require 'mysql'
handleDisconnect = () ->
connection = mysql.createConnection
host: 'localhost'
user: 'root'
password: 'passroot'
database: 'mydb'
connection.connect (err) ->
if err
console.log 'Error connecting to db: ', err
setTimeout handleDisconnect, 2000
connection.on 'error', (err) ->
console.log 'db error', err
if err.code == 'PROTOCOL_CONNECTION_LOST'
handleDisconnect()
else
throw err
#instance = connection
#
module.exports = handleDisconnect
Although I'd personally just do the following, don't bother with "instance" at all:
Use #connection in your function
Scrap the #instance = connection
Get the function to return itself
Access it with require('../database')().connection.

Node.js MySQL connection not being released properly

I think my connections aren't being released properly. Sometimes I get an error stating that my pool has reached its limit. Also, sometimes accessing the db randomly takes 15+ seconds. Whenever I check how many connections are in use using pool._allConnections.length, it never returns anything above 60. Here is my code:
const mysql = require('mysql');
const config = require('./config.json');
const pool = mysql.createPool({
connectionLimit : 999,
host: config.host,
user: config.user,
password: config.password,
database: config.database
});
const db = (() => {
_query = (query, params, callback) => {
pool.getConnection((err, connection) => {
if (err) {
callback(null, err);
} else {
connection.query(query, params, (err, rows) => {
connection.release();
if (!err) {
callback(rows);
} else {
callback(null, err);
}
});
}
});
};
return {
query: _query
};
})();
module.exports = db;
i've faced same issue and https://github.com/mysqljs/mysql/issues/1518 help me. Notice line
Yeah, that was the issue. I was calling mysql.createPool on each
query.
Actually you are importing db from query.js (let say your post code) to fire a query. every time you fire a query it create a new pool.to solve this issue you can put createPool code block to app.js and can share it global or can use in query.js via any other code style.
Referring official doc https://github.com/mysqljs/mysql#pooling-connections find line
Since the pool.query method is a short-hand for the pool.getConnection
-> connection.query -> connection.release() flow, calling pool.end() before all the queries added via pool.query have completed,
later i used this to stop headache of release connection

How to get Data from MongoDb using mongoose?

I just started learning MongoDB and mongoose. Currently I have the following structure:
database -> skeletonDatabase
collection -> adminLogin
When I run db.adminLogin.find() from the command line I get:
{ "_id" : ObjectId("52lhafkjasfadsfea"), "username" : "xxxx", "password" : "xxxx" }
My connection (this works, just adding it FYI)
module.exports = function(mongoose)
{
mongoose.connect('mongodb://localhost/skeletonDatabase');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log('Conntected To Mongo Database');
});
}
My -js-
module.exports = function(mongoose)
{
var Schema = mongoose.Schema;
// login schema
var adminLogin = new Schema({
username: String,
password: String
});
var adminLoginModel = mongoose.model('adminLogin', adminLogin);
var adminLogin = mongoose.model("adminLogin");
adminLogin.find({}, function(err, data){
console.log(">>>> " + data );
});
}
My console.log() returns as >>>>
So what am I doing wrong here? Why do I not get any data in my console log? Thanks in advance for any help.
mongoose by default takes singular model names and pairs them with a collection named with the plural of that, so mongoose is looking in the db for a collection called "adminLogins" which doesn't exist. You can specify your collection name as the 2nd argument when defining your schema:
var adminLogin = new Schema({
username: String,
password: String
}, {collection: 'adminLogin'});
Had a problem with injecting it within an express route for my api so I changed it thanks to #elkhrz by first defining the schema and then compiling that one model I want to then pull like so:
app.get('/lists/stored-api', (req, res) => {
Apis.find(function(err, apis) {
if (err) return console.error(err);
res.send(apis);
});
});
I wouldn't send it to the body, I would actually do something else with it especially if you plan on making your API a production based application.
Run through this problem and read up on possible proper ways of rendering your data:
How to Pass Data Between Routes in Express
Always a good idea to practice safe procedures when handling data.
first compile just one model with the schema as an argument
var adminLogin = mongoose.model('adminLogin', adminLogin);
in your code adminLogin does not exist, adminLoginModel does;
after that ,instead to
adminLogin.find({}, function(err, data){
console.log(">>>> " + data );
});
try this
adminLogin.find(function (err, adminLogins) {
if (err) return console.error(err);
console.log(adminLogins);
is important the "s" because mongo use the plural of the model to name the collection, sorry for my english...

Categories

Resources