MongoDB. Studio3T. Mongoose. Hyper returns empty array. Pluralization in naming most likely not the issue - mongodb

I ran mongod in Hyper Terminal.
I established connection with Studio3T and MongoDB. And created collection and documents in Studio3T. The database, collection, and documents appear in Studio3T and MongoDB. In Hyper the database and collection exist, but the documents are not there. Any idea why?
I have consulted stackoverflow and could not find the answer in the resources listed (sorry if I missed something):
Mongoose always returning an empty array NodeJS
Model.find() returns empty in mongoose
Mongoose always return empty array?
What are naming conventions for MongoDB?
MongoDB:
Mongosh in Hyper:
Studio3T:
Code in Atom:
Run code and result in Hyper:

I started mongod and connected to the MongoDB server locally. My application code pointed to the following:
mongoose.connect("mongodb://localhost:27017/wikiDB");
The above is local. But my data is on the cloud-based MongoDB Atlas Cluster. I shouldn't need to run mongod. So, I changed my application code:
mongoose.connect("mongodb+srv://admin-yourname:password#blahblahblah.mongodb.net/wikiDB");
where yourname AND password AND blahblahblah are specific to the coder.
Now my application code points to the Atlas Cluster where my data is.
Summary:
My data is on the cloud-based Atlas Cluster.
To see my data on the Atlas Cluster from the command line, I "Connect with the MongoDB Shell" and run the connection string that MongoDB provides in the command line. Now I see my data in the command line.
To see my data on the Atlas Cluster when I run my code, I use "Connect your application" and place the connection string that MongoDB provides in my code (the mongoose.connect(....) ). Now when I run my code, I can see my data in my browser or command line if I console.log it.
If you are in the command line and run mongod in one tab, and mongosh in another, and use db.articles.find(), then nothing will appear but an empty array. That's because the data is on the cloud-based Atlas cluster. In this instance, you are viewing things locally.
If your application code points to your local MongoDB server (ran mongod in one tab, mongosh in another), then when you run the code you will see nothing but an empty array because the data is on the cloud-based Atlas cluster. In this instance, you are viewing things locally.

Related

How to transfer a database from MongoDB Compass to MongoDB Atlas

I have an existing database for a discord bot in MongoDB Compass v1.28.1 I want to transfer all the data in the database to mongodb atlas because of its more extensive functionality and to not have to wait for compass to take ages to load each time I open it. However when I follow the steps to connect that are provided in Atlas, the pop-up that's supposed to appear when I copy a path to the clipboard doesn't appear, and nothing happens. I tried to connect through my app in VSCode, the same way I did for Compass, using mongoose. Still no collections are loading or any data being stored. I have made my schemas etc. which work perfectly fine in Compass...
Migration to Atlas is documented at https://docs.atlas.mongodb.com/import/
To save you some reads, you have to options - export/import and mongodump/mongorestore.
I would recommend to try export/import first. It's built into Compass https://docs.mongodb.com/compass/current/import-export/ and must be simpler to use considering limited experience with mongo. It's UI oriented so just follow the click-through guide in the documentation.
Unfortunately it has some limitations related to data type conversion from BSON to JSON and may be a bit tedious if you have large number of collections.
In this case you will need to follow CLI mongodump/mongorestore way #barrypicker suggested in the comments. Both commands are available in cmd and PowerShell consoles.
First you backup your local database https://docs.mongodb.com/v4.2/reference/program/mongodump/:
mongodump --uri="mongodb://username:password#localhost:27017/discordbot"
username and password are the ones you use in compass to connect to the source database.
It will create dump directory with all collections you have.
Then you have to upload the backup to Atlas:
mongorestore --uri="mongodb+srv://username:password#cluster.tenant.mongodb.net/database" dump/
username and password are the ones you use to connect to atlas cluster, listed in the "Security/Database Access" section.
You can get the exact subdomains for the --uri part from Atlas. In the dashboard click "Connect" button for the cluster you want to connect to, then choose "shell" as the connection method in the connection pop-up:

Mongodb not listing collections when connecting remotely from CentOS

Mongo server: Windows 10 (host)
client: CentOS 6.2, a virtual box vm on windows 10 host. This is actually a cloudera quick start vm.
Issue:
mongodb connects to the remote server (from CentOS to Windows) via terminal, lists the databases fine, but 'show collections' just returns blank. That said, the collections are accessible because I can query any collection and the count also gives me the correct results.
On the other hand, I have connected to the same mongo server from IntelliJ and it shows all the collections just fine.
Just curious as to why this is happening.. Any comments?
Side Note: is there a mongodb command to count the number of collections in a database?
Thanks
_Vamsi
Make sure you are using the database you want to show the collections for. You may be using a database that doesn't have any collections.
> use desiredDatabase
> show collections
If the list is still empty, try signing in with an admin user account. The user needs to be able to perform the listCollections action. The dbAdmin role includes the listCollections action.
To get the count you can use the getCollectionNames function which returns an array and you can get the length from that.
> db.getCollectionNames().length

how to show my users collection with mongodb

I'm sorry for this (peraphs) stupid question ... I install meteor and mongodb in my windows computer and i start to write some apps. I don't understand how to use mongo for shoving my db app ... i open one shell in my app dir and launch mongod, in one more shell in the same folder i start mongo.
show dbs
local
use local
switched to db local
show collections
startup_log
system.indexes
Where are my collections? Where is users collection?
When your app is running use this command on a separate command line mongo 127.0.0.1:3001
Meteor keeps the collections in this server. After you run mongo on this server, by writing use meteor you can use db specific to your running app. And then you can display your collections with db.getCollectionNames()
Meteor uses a library called Minimongo that's why it doesn't display if you run show dbs on your mongo shell.
By default it points to port 3001 hence if you are using Robomongo you can just make the set up to watch that port.
To display all your MongoDB collections using the shell, you may check this answer:
How to list all collections in the mongo shell?
You may also use a MongoDB GUI Tool such Robomongo

How can I query an FS collection in Meteor from the command line?

It is very useful to run meteor mongo and query collections from the command line, for debugging purposes, etc.
Recently, I have added the collectionFS package to enable image storage in the database. However, I am unable to query the database from the command line.
db.fs.collection_name.find() is not doing the trick, and I can't seem to find the correct command anywhere.
Go to the Meteor Mongo console: meteor mongo
See all the collections that are available: show collections
Look for the one that has cfs.collection_name.files
Choose the one that has your collection name. For example, I'm using collectionFS with gridFS for images. When I type show collections, I see cfs_gridfs.images.files so I just do: db.cfs_gridfs.images.files.find() to see those files.
Hope that helps.
If you find it difficult to use the command line or terminal, you have a UI for MongoDB called Robomongo which is easy to install and use. I use Meteor with its default port number and then in Robomongo it is used as 3001.
And the query to view collection here is same as db.collection_name.find().

User field missing in system.profile collection when connecting with Mongos

We have a MongoDB cluster and clients connecting to it through a Mongos instance. The individual mongo(s) in the cluster are all running with --auth, and the Mongo use a --keyfile when communicating with them. We are profiling slow queries but are not getting the user names on queries that go through Mongo.
To make it clearer:
If I connect directly to one of the Mongo, authenticate, and run a query, then I can look in the system.profile collection afterwards, and the user field will be populated with my username.
If I connect through mongos, authenticate, and run a query, then the system.profile collection contains profiling info about the query, but the user field is blank.
The authentication is required, I can't run a query through Mongo without authenticating first, but the user name just doesn't seem to be included in the profiling info, and we'd really like to be able to see it.
Any ideas? Any alterations I can make to our configuration?
Just to actually add an answer:
As Ren stated in his comment, he filed a ticket, as this is related to a bug.