Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

Killeon Patterson
Killeon Patterson
18,527 Points

Updating data to MongoDB (mongoose)

I've made my Schema and created a seeder file to upload data to my MongoDB server, but the data appears to remain unsent. I've restarted npm, run mongo, and mongod in seperate consoles (windows 10 machine). When I run 'use shopping' , then db.shopping.find(), nothing populates the console.

Here is a link to my gist https://gist.github.com/Satellite9/5e4ce3de5c19cee2f355d872b6d7d3c8

Killeon Patterson
Killeon Patterson
18,527 Points

Hi Florian, the command line just moves down and displays nothing. I get the same response when I use "show collections." I changed my systems variable path to include the 'seed' directory. When I run command 'node product-seeder.js,' in the console, I receive a depracationwarning. Thank you for your reply.

3 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Killeon,

How does the product-seeder know which database it connects to? The connection string to the database is in the app.js file as far as I can see. I think if you start the connection in your seeder and then run it, you should have the data populated.

Regards
Andrew

Killeon Patterson
Killeon Patterson
18,527 Points

Hello Andrew,

Thank you for your reply. It resolved my issue.

Did the connection string need to go into seeder, as opposed to the app.js file, because the seeder is not technically part of the app? Because my understanding was the app.js was the place where I needed to require all packages that pertain to the application. Thank you again.

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

app.js is just an entry point in to code that you want to run for the server. If you create other files that perform different tasks, mini-apps if you will, you need to establish their own connections to databases or what ever it needs to do. Your seeder had no idea of what the app.js is doing because it's not require()d.

Seth Kroger
Seth Kroger
56,413 Points

The problem is you are using the database name instead of the collection's name for find(). Since your Mongoose Schema is named Product your collection should be named products and the find command should be db.products.find().

Killeon Patterson
Killeon Patterson
18,527 Points

Hi Seth, That is the syntax that I'm using, it does not appear to resolve the issue. Thank you for your reply.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Killeon;

I'd be interested to see what information you get back from

show databases

as well.

Killeon Patterson
Killeon Patterson
18,527 Points

Hello Ken,

The 'show databases' command showed and empty admin and an empty local. The problem was the location of my connection string. It was in my app.js file and not my seeder. Although, my understanding, when I created the app.js, was that the everything had to run through that file first and always. I didn't realize the string was suppose to go into the seeder.

Thank you for your reply and time.

Ken Alger
Ken Alger
Treehouse Teacher

Great that you resolved the issue.

For future trouble shooting, if doing a show databases or show dbs doesn't result in showing the desired database, that is an indication that the specific DB hasn't been created.

Remember that MongoDB doesn't create a database or collection until a document is created in the database.

Again, I'm pleased it all worked out.

Happy coding.