forked from tugumeandree/mongoDBCrushCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (41 loc) · 1.54 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// In this course we are adding and showing added blog articles in a mongo database
const mongoose = require('mongoose');
require('dotenv/config');
// request connection with local serever
mongoose.connect('mongodb://localhost/yourDatabaseName', { useNewUrlParser: true, useUnifiedTopology: true });
// {useNewUrlParser: true,useUnifiedTopology: true }flags for deprecations in the mongodb driver
// connection with cloud based serverless MongoDB Atlas //use dotenv to hide credentials
// mongoose.connect('process.env.DB_CONNECTION');
// in dotenv file DB_CONNECTION =mongodb+srv://userName:<password>@freecluster-t1jay.gcp.mongodb.net/test?retryWrites=true&w=majority
// establish the connection
const db = mongoose.connection;
// use events once open and on error events to show db status
db.once('open', () => {
console.log('Your db is connected');
});
db.on('error', (err) => {
console.log(err);
});
// use mongoose.model to create and use models with schemas
// Use internal model
// const Article = mongoose.model('Article',{
// title:String,//short hand for type casting //you can also use
// author:String
// });
// Or Use external model
// external models mongoose.Schema with mongoose.model()
const Article = require('./models/articles.model');
const article = new Article({
title: 'Design Sprint',
author: 'Google',
});
// save the blog article in the databse
article.save();
// show all the blog articles in the databse
Article.find((err, articles) => {
if (err) {
console.log(err);
} else {
console.log(articles);
}
});