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

73 Views Asked by At

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):

  1. Mongoose always returning an empty array NodeJS
  2. Model.find() returns empty in mongoose
  3. Mongoose always return empty array?
  4. What are naming conventions for MongoDB?

MongoDB: enter image description here

Mongosh in Hyper: enter image description here

Studio3T: enter image description here

Code in Atom: enter image description here

Run code and result in Hyper: enter image description here

1

There are 1 best solutions below

0
user237736 On

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:[email protected]/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.