winston-mongodb error after initializing - node.js

I'm trying to configure a basic winston logger but keep getting the same error.
All I have so far to configure it is this
var winston = require('winston');
var mongoLog = require('winston-mongodb').MongoDB;
var appSettings = require('./appSettings');
var logger = new (winston.Logger)();
logger.add(mongoLog, {
db: appSettings.database,
host: appSettings.dbConnection,
collection: appSettings.loggingCollection
}
);
This is the error I'm getting.
winston-mongodb: error initialising logger Error: invalid schema, expected mongodb
The host is the ip of a mongodb instance in azure, but that doesn't seem to be the issue because if I remove host (defaulting it to localhost according to the docs) and try to have it connect to my local mongo instance it gives the same error. It also doesn't seem to matter if I call any methods on the logger or not.

Was looking at the wrong documentation I guess?
On this page it has the db param described like this
db: The name of the database you want to log to.
So I thought I would have the db name there and would need to specify the host separately.
But on this page it has a different description.
db: MongoDB connection uri, pre-connected db object or promise object which will be resolved with pre-connected db object.
Which apparently was the correct description, the full URI being this format:
mongodb://<host>:<port>/<db>

Related

How to check if Redis in nodejs app is working

I installed redis and mongodb in VM, I'm trying to speed the response time of my mongoose find() requests .. I used lean() and it's working good .. then I found redis and I installed it and I followed this tutorial to set up redis in my controllers :
https://epsagon.com/development/using-redis-to-optimize-mongodb-queries/
I created the cache.js in /services with connection details and then in my controller code I imported :
const { clearKey } = require("../services/cache");
And in my find() I added .cache() :
await Book.find({ author: req.query.author }).cache();
I want to know how the app is knowing the .cache() function is my redis server because I only imported "clearKey " and I didn't use it ? And the performance is not speeded so I don't know if the Redis set up is correct and working or not. How to check that ?
Thanks
You added the cache function to mongoose.Query.prototype.cache, it means that you extended mongoose.Query with cache function and it was automatically added to all Mongoose models.
Regarding your second question, you need to add to your Redis:
const client = redis.createClient({
host: keys.redisHost, // make sure its your redis host
port: keys.redisPort, // make sure its your redis port
retry_strategy: () => 1000
});
When your query is running over MongoDB you should see a log:
console.log("Response from MongoDB");
And when your query is running over Redis you should see a different log:
console.log("Response from Redis");
You can see more the this github file.

How to add MongoDB Atlas cloud database in KeystoneJS v5

I have database server on MongoDB Atlas Cloud.
I can Setup Keystonejs 5 with MongoDB local database but I don't to what Keystonejs expect for database connection string and settings.
below detail may useful to understand my issue
Mongoose Database Adapter
Usage
const { MongooseAdapter } = require('#keystonejs/adapter-mongoose');
const keystone = new Keystone({
name: 'My Awesome Project',
adapter: new MongooseAdapter(),
});
API new MongooseAdapter(options)
options.mongoUri (optional)
This is used as the uri parameter for mongoose.connect().
Default: Environmental variable (see below) or 'mongodb://localhost/<DATABASE_NAME>'
If not specified, KeystoneJS will first look for one of the following environmental variables:
CONNECT_TO,
DATABASE_URL,
MONGO_URI,
MONGODB_URI,
MONGO_URL,
MONGODB_URL,
MONGOLAB_URI,
MONGOLAB_URL
If none of these are found a connection string is derived with a DATABASE_NAME from the KeystoneJS project name.
Mongoose Options (optional)
Additional Mongoose config options are passed directly through to mongoose.connect().
See the Mongoose docs for a detailed list of options.
You just need to add an environment variable that matches one of the ones listed in the question and then set that environment variable to be the connection string that MongoDB atlas cloud gives you.
I added a .env file to my project and then added an entry for MONGO_URI there, then used the dotenv package.
You can do it by adding .env file or you can directly pass mongoUri to adapter.
See the code below:
const keystone = new Keystone({
name: 'My Awesome Project',
adapter: new MongooseAdapter({mongoUri:"mongodb+srv://....."}),
});

How to make connection with 2 DBs while using mongo official driver for node js

Am trying to make the connection from node js to Mongo DB.
while using mongo's official node js driver Official
I am able to make a connection with DB at one at a time.
but am looking for a way to get connected with 2 DBs at a time.
for a fallback issue.
Approach one:
const MONGO_CLIENT = require('mongodb').MongoClient;
const MONGO_URL = mongodb://USER:PASS#HOST:PORT/DBNAME
MONGO_CLIENT.connect(MONGO_URL, (err: any, db: any) => {
if (err) {
res.status(503).send(err);
}
console.log ('Connected do the things');
}
I got success by this way.
but am looking for a fallback way.
while setting two Hosts in URL I got errors
const MONGO_URL = mongodb://USER:PASS#PRIMARY_HOST:PRIMARY_HOST_PORT,SECONDARY_HOST:SECONDARY_HOST_PORT/DBNAME
am getting following errors
{
"name":"MongoError",
"message":"seed list contains no mongos proxies, replicaset connections requires the parameter replicaSet to be supplied in the URI or options object, mongodb://server:port/db?replicaSet=name"
}
Could anyone please explain to me what am doing wrong. and how should I connect with multiple DBS?
NOTE: Primary and secondary DBs are same and always synched.

What is the correct URL schema for connecting to mongodb with multiple databases

Setting up a new project, I wanted to have separate databases for test, dev and prod:
d:/mongodb/project/test
d:/mongodb/project/dev
d:/mongodb/project/prod
I got these up with mongod --dbpath d:/monodb/project/<env>
When I try to connect I get Error: More than 1 database name in URL
const { MongoClient } = require('mongodb')
MongoClient.connect('mongodb://localhost:27017/project/dev')
The example given in the api docs doesn't help much
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
// Connection url
var url = 'mongodb://localhost:27017/test';
// Connect using MongoClient
MongoClient.connect(url, function(err, db) {
What is the correct specification for the url connection? (Or, if I am going about this the wrong way entirely, what is the best way to separate databases for testing?)
You can connect to mongodb using this driver as instructed in their documentation:
http://mongodb.github.io/node-mongodb-native/2.2/quick-start/quick-start/
So the URL you have there is correct.
If you want to have separate databases (which could also be on different hosts with different credentials) then I suggest you use a config package:
https://www.npmjs.com/package/config
This allows you to define a configuration for each environment where default will be a catch all if environment variable cannot be matched to a json file. In other words, NODE_ENV=prod would map to prod.json, NODE_ENV=test would map to test.json and NODE_ENV=[empty] would map to default.json. This is one possible setup.
You definitely don't want to create multiple connections for each environment. This is not necessary.

MongoError : Connect failed - Bitnami AWS instance

I am trying to connect to my AWS Bitnami MEAN instance. The code has been uploaded on the server. But on connecting to it, I am getting the following error :
I have been following the steps given at : https://scotch.io/tutorials/deploying-a-mean-app-to-amazon-ec2-part-1 to connect to the instance.
In the second aprt of it, where they are making changes for mongoDB, I did all those, but since then, I am getting this error.
Your connection is being refused, it seems like an authentication error. You must provide the password in your application, i.e. if you are using Mongoose in order to connect:
var Mongoose = require('mongoose');
var db = Mongoose.createConnection('mongodb://*USER:PASSWORD*#localhost/*DATABASE*');
You must use your correct mongodb socket and your correct password and database name.
Best regards,
Silvio Fernández

Resources