How to list MongoDB items of a Local Database? - node.js

I've already done my research and have gotten so far...
By entering the commands on mongoDB shell or node, this shows up:
Now the problem is that I actually have users in this DB because I'm testing this on an application on port 3000 localhost. When I try to register the same user a second time, an error shows up, saying that this entry is duplicate.
TL;DR: Mongo says DB is empty when it's not

When you're in the database testForAuth, use the command db.users.find() and it should print out all the users.

Related

MongoDB Connections Increasing, Even though i have disconnected all my resources from mongo DB

enter image description here
I have been using mongo db as my database , suddenly mongo db mails me that i have used maximum connection i.e 500 but i have only connected to a single application which iam working. even after stopping my api and mongo compass it shows some kind of random active connections , how to reduce the active connections to actual active connection??
There are only few things to solve it.
Need to add the below options in your Mongo URI,
connectTimeoutMS
socketTimeoutMS
maxIdleTimeMS
For my Case I have configured with below number
<mongo_uri>?retryWrites=true&w=majority&maxIdleTimeMS=60000&connectTimeoutMS=150000&socketTimeoutMS=90000
With these options i have solved the issue.

use custom mongoDB server instead of mongolab

Today i started migrating from firebase to mongoDB,
I used this tutorial and its all up and running http://thejackalofjavascript.com/re-architecting-a-firebase-app-in-node/
When checking the code i see there is a mongolab link connected to the code,
mongodb://admin:admin123#ds061620.mongolab.com:61620/testsync
My question is: can i easily setup my own local database to use instead of this? and what packages i would need to do this?
The main reason for switching to mongo instead of firebase is the pricing, please take this into account.
Yes, of course you can.
First, install mongodb locally, to do that, follow the instructions for the distribution you're working on.
Then, make sure your mongodb service is running.
After that, on your database connection script, change the connection parameters.
I guess that you have something like this on your .js file:
mongodb://admin:admin123#ds061620.mongolab.com:61620/testsync
Just to make a connection test, try to change it to:
mongo://localhost/test
After a successful connection, you can start to manage your database as you want.
As additional information, you don't have to specify user, password and a different port than the mongo's default, because you're using your local configuration, if you want to do so, you have to configure your mongodb server to make it work that way.
I'm working with mongoose ORM to manage a local database, and here's my connection function working.
Mongoose connection to local database
Hope this helps you.

Parse Server "Unable to ensure uniqueness for usernames"

Me and my partner are having serious issues with MongoDB these days. Suddenly, our Parse Server mounted on MongoDB, in a VPS, started failing. Our app couldn't connect there, nor the Parse Dashboard. Something was failing.
In the parse logs, this is what appears:
{"level":"info","message":"Parse LiveQuery Server starts
running","timestamp":"2017-07-09T20:23:59.711Z"}
{"level":"info","message":"Parse LiveQuery Server starts
running","timestamp":"2017-07-09T20:25:47.884Z"} {"message":"Unable to
ensure uniqueness for usernames:
","name":"MongoError","stack":"MongoError: failed to connect to server
[localhost:27017] on first connect\n at null.
(/home/main/StepApp/node_modules/mongodb-core/lib/$ {"message":"Unable
to ensure uniqueness for user email addresses:
","name":"MongoError","stack":"MongoError: failed to connect to server
[localhost:27017] on first connect\n at null.
(/home/main/StepApp/node_modules/mongod$
We tried this whole bunch of things:
Installing a new MongoDB version
Import the old .db files
Restart our Node.js instancies via pm2
Adding more space to our VPS (it seems MongoDB was having some trouble with disk space, but this is already fixed).
Adding an admin user to our db, with all permissions, and then adding it to the mongodb connection string in our Node file.
After all of this, several server restarts, pm2 restarts, mongo restarts...
... nothing works.
When I restart the server, I receive that strange error string I copied before, though Mongo is working fine.
Seems to be more of a Parse problem... but, why?
Any help will he appreciated!
EDIT / UPDATE:
I've been checking if MongoDB is working, and it's service "mongod" is online. Tried to listen to a different port, changed it too in the index.js mongo connection string, but I keep getting the same error.
Strange thing is, that when I connect to my database with my newly created Mongo user, it authenticates correctly! (I could see it in the logs). So it seems to be a Parse error, somehow it doesn't get the Mongo response or something like that.
What do you think?

mongoose create DB not working

Neither mongoose.connect('mongodb://localhost/sync-db'); nor mongoose.createConnection('mongodb://localhost/sync-db'); is creating the sync-db on local host.
Though both connection callbacks are successfully connected. But when I tried to check both robo-mongo and mongod client console using the command show dbs, the sync-db is neither to be seen on robo-mongo nor on mongod client
WTF?
Since no one is answering this question, I will post my own answer based on what #Odonno said on the comment, which is correct.
The codes above use to create database is not wrong, in the background the database has already been created, but for some reason (I hope someone will explain this) you wont see it until you added some data to it.

How do I connect to the same MongoDB database with node.js and the console?

Using mongoose I connect to a Mongodb database on my local machine. I'm using mongoose.connect('mongodb://localhost/myTestDB');. Then in a console I enter the Mongodb shell with mongo. However, I can't find the myTestDB database inside the mongodb shell with the show dbs command. What gives? How do peruse the same database nodejs is connecting to in the shell?
Your database should atleast have one document in the collection, only then it will be listed in the mongo shell.
From the comments and your description it seems you aren't starting MongoDB through mongod. You first need to establish a connection to your database file (should have journal , "databaseName" .0 and .ns, mongod.lock and storage.bson files inside). Setting up your dbpath alone won't connect to it unless you explicitly do so in console.
If you don't want to store your database file locally you can use mongolab.
Follow this manual (Run MongoDB section) and it should work.

Resources