简体   繁体   中英

Why doesn't my database doesn't show up in mongodb?

Currently trying to learn backend development using node.js.

I followed the instructions for installing mongoDB, and moved the contents of the bin files into /Users/my-username/mongodb.

Then I did

mkdir -p /data/db

Finally I have these lines of code in the main app.js file,

mongoose.Promise = global.Promise;
// Connect to mongoose
mongoose.connect('mongodb://localhost/vidjot-dev', {
        useMongoClient: true
    })
    .then(() => console.log('MongoDB Connected...'))
    .catch(err => console.log(err));

...

When I run the program I get the mongoDB connected... I also wrote the idea schema, and the process form,

// Process Form
app.post('/ideas', (req, res) => {

    const newUser = {
        title: req.body.title,
        details: req.body.details
    }
    new Idea(newUser)
        .save()
        .then(idea => {
            res.redirect('/ideas');
        })

});

However when I run mongo in the terminal, only this appears,

admin   0.000GB
config  0.000GB
local   0.000GB

instead of this, that appears in the tutorial I'm using

admin       0.000GB
config      0.000GB
local       0.000GB
vidjot-dev  0.000GB

what did I do wrong?

You'll have to hit the endpoint of /ideas before the database, collection and documents all get generated dynamically.

Just connecting to mongodb with the js driver won't create anything, unless you explicitly call createDatabase ``createCollection`.

我通过在http://localhost:5000/ideas/add路由中创建标题和详细信息解决了这个问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM