0

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

user237736
  • 101
  • 1
  • The fact that `mongosh` is not returning any results looks somewhat suspicious. Is there an invisible character in the collection name? What happens if you run ` db.getCollection(db.getCollectionNames()[0]).find()` in the `mongosh` shell (after `use wikiDB`)? – user20042973 Oct 19 '22 at 17:58
  • I was not connected to the MongoDB server on the Atlas cluster with my application. I was running mongod and connecting to the MongoDB server locally. – user237736 Oct 20 '22 at 15:10
  • Are you saying that's why the `mongosh` results were empty or that you solved the problem by changing where the application was pointing? – user20042973 Oct 20 '22 at 15:11
  • Where the application was pointing. Mongosh was empty because everything was local. I connected to the MongoDB Shell by running the connecting string in the command line. Then I could see my wikiDB and the collection and documents. Then I changed where the application was pointing; I changed the mongoose.connect(....). It was pointing locally; but now I used the connection string from MongoDB (connect with your application). Works fine. – user237736 Oct 20 '22 at 15:20

1 Answers1

0

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.

user237736
  • 101
  • 1