server crushes after linking mongo atlas, - node.js

Server crushed when i linked mongo atlas on the server.js file,error being generated is connection.once is not a functionserver.js file
I have tried to require once as an eventemitter but with no success

I think the way you have implemented this is wrong. When I am looking at another example i.e Mongoose.connection('once') what does it mean it has applied db.once on a DB where as you are doing it on a connection

Related

Handling Mongoose connection failure in NestJS

I am new to NestJS.
I am trying to connect to mongo instance, like this MongooseModule.forRoot('mongodb://localhost:27017/demo')
How can I handle no connection here.
Probably you wrote wrong database URL.
You have to follow official documentation:
Mongo

MongoLab and Heroku without mongoose

I am currently working with a team in a NodeJS project that uses express and a local MongoDB database.
We did not implement mongoose into the project, and now we are at a stage where we wish to upload the project using heroku, so that we can have a central point as we further develop different parts of the project (apps and web pages that consume the data from the database.)
We managed to upload the project to heroku, and some of the pages are working, but we can't access the database, it returns "500 - internal server error" everytime we try to access data from the MongoDB.
After some research we could only find that it's best to use MongoLAB to store the data in a cloud based datacenter, but we can't seem to find ways to connect our project with MongoLab without the use of mongoose, which we didn't install to the project.
My question is: is there a way to connect our NodeJS project to MongoLab using only the original MongoDB npm module, without having to install and use mongoose? We are at a stage that refactoring our DB-access classes, and implement mongoose, would cause our project's deadline to be delayed.
Thank you for your time.
You can definitely use mongoDB with mLab(previously mongoLab), without using mongoose.
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://ID:PASSWORD!#SUBDOMAIN.mlab.com:PORT/DATABASE_NAME', function(err, db) {
console.log("Connected correctly to server.");
db.close();
});

Mongoose calls hangs

I haven't worked on my PC for few days.
Suddenly all the calls to mongo via mongoose hangs up, the callbacks are not called.
I checked that my call to .connect works, and that the connection state is 1 (connected).
I also made sure mongo service is running on localhost and the appropriate port 27017, and I can use the mongo console and query the db manually.
I also scanned the Internet for solutions but all I found was 'check that you're actually connected', and I verified that already.
Mongoose version 2.15.0, mongo version 2.4.9 and node js version is 4.4.2.
I fixed it.
Problem was duplicate references to the mongoose module.
I had a mongoose reference locally (which was connected), but my schema was present higher in the node_modules hierarchy, and it have used another mongoose instance which had no connection.
Once I removed the duplicate mongoose modules (npm uninstall mongoose one of them) it worked.
Above solutions didn't work for me so I fixed with following solution.
I had same issue where my db calls used to hang with no invocation to my callbacks or the promise resolution.
The problem was I used "createConnection()" to establish the connection with the db. But it didn't work perfectly.
Instead using "connect()" and "connection" imports worked.
Here is the sample code. Hope this helps.
import { connect, connection } from "mongoose";
const mongoUri = `mongodb://${my_mongo_host_&_port}`;
connect(mongoUri, {}); //to connect to my standalone db
//"connection" to listen to events
connection.on("connected", () => {
console.log("MongoDB connection established!", mongoUri);
});
I am working with "mongoose": "^6.6.1".

Link mongoDB instance database to meteor local

I've recently setup an ec-2 instance to deploy a meteor application on AWS. My app works correctly with mongoDB database.
Now I want to connect my meteor project's database to the EC2 database and then I've tried some command lines :
start my meteor project with a new mongo url with the following command line : "MONGO_URL="mongodb://username:password#xx.xx.xxx.xxx:27017/Tasks" meteor" but it returns the following screen
connect to mongo with the other following command line :
"meteor mongo --url xx.xx.xxx.xxx:27017" but it returns a timeout. I thought it was due to the lack of username and password however when I add this option meteor does not assume the command.
connect to mongo in my js collection file with
"export const Tasks = mongoose.connect("mongodb://username:password#xx.xx.xxx.xxx:27017/Tasks").connection;" but it returns "Error: connect ECONNREFUSED" (second screen)
Is there any tips to manage to establish database connection from local meteor ?
Thank you for answer,
Martin
If the database and the Meteor processes are in the same machine, you can call it from "localhost".
MONGO_URL="mongodb://user:password#localhost/Tasks"
If you really want to use the IP + Port approach, you have to ensure that it's accessible from the EC2 Security Groups you're using.
The same applies for connecting manually in Mongoose but you should't be using Mongoose with Meteor, it has it's own way of handling collections. If you need something more powerful, or a way to extend it yourself checkout the Meteor Simple Schema project.

Correct Use of Mongoskin

I usually work with mongoskin because I like to be close to the database. Usually, I do a setup with a file like db.coffee, that contains just this:
mongo = require 'mongoskin'
# either local
module.exports = mongo.db 'mongodb://localhost/database'
# or remote
module.exports = mongo.db 'mongodb://<user>:<pass>#<host>:<port>/<db>?auto_reconnect=true'
Then I use it in my other sources:
db = require 'db'
users = db.collection 'users'
# Now use the collection in handlers and middleware
This seems to work perfectly fine when I am using a local mongo server, I've had an uptime for months and it never turned out to be a problem.
However, when I am using the remote second, I get problem if the server runs longer than just a few minutes - the connection to the mongodb seems lost, despite auto_reconnect. I guess this is because the localhost connection is never closed automatically.
However this led me to thinking if I am maybe using mongoskin in a wrong way, or if there's simply a bug with the auto_reconnect?
ensure mongoskin is using the 1.0.0 or higher driver

Resources