1const mongoose = require('mongoose');
2
3mongoose.connect('mongodb://localhost:27017/testDB',
4 {
5 useNewUrlParser: true,
6 useUnifiedTopology: true,
7 useCreateIndex: true,
8 useFindAndModify: false
9 })
10.then(()=>{
11 console.log('connected');
12 })
13.catch((e)=>{
14 console.log("Something went wrong", e);
15 })
16
17
1const ToySchema = new Schema({ name: String });
2const ToyBoxSchema = new Schema({
3 toys: [ToySchema],
4 buffers: [Buffer],
5 strings: [String],
6 numbers: [Number]
7 // ... etc
8});
1import express from 'express';
2or
3const exporess = require('express');
4import mongoose from 'mongoose';
5or
6const mongoose = require('mongoose');
7const app = express();
8mongoose.connect('mongodb://localhost:27017/mongo-tree' , {
9 useNewUrlParser: true,
10 useUnifiedTopology: true
11});
12
13const db = mongoose.connection;
14db.on('error', console.error.bind(console, 'connection error:'));
15db.once('open', function() {
16console.log('connected db')
17});
18app.use('/', (req, res) => {
19 res.send('hello');
20}
21);
22const port = process.env.PORT || 5000
23app.listen(port, () => {
24console.log('running');
25})
26//for es6 >npm i @babel/core @babel/node @babel/preset-env --save-dev
27
28//create .babelrc file and put this
29{
30 "presets": [
31 "@babel/preset-env"
32 ]
33}
34//and package.json
35 "scripts": {
36 "test": "echo \"Error: no test specified\" && exit 1",
37 "start": "nodemon --exec babel-node server"
38 },
1const mongoose = require('mongoose');
2mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true, useUnifiedTopology: true});
3
4const Cat = mongoose.model('Cat', { name: String });
5
6const kitty = new Cat({ name: 'Zildjian' });
7kitty.save().then(() => console.log('meow'));
1const mongoose = require('mongoose');
2
3mongoose.connect('mongodb://localhost:27017/twitter', {
4 useNewUrlParser: true,
5 useUnifiedTopology: true
6});
7
8const db = mongoose.connection;
9
10db.on('error', console.error.bind(console, 'connection error:'));
11db.once('open', function () {
12 console.log("Database Connected !!!");
13});
14
15module.exports = db;
1const mongoose = require("mongoose");
2
3// use .env to connect
4
5const connectDB = async () => {
6 try {
7 const conn = await mongoose.connect(process.env.MONGO_URI, {
8 useNewUrlParser: true,
9 useUnifiedTopology: true,
10 });
11 console.log(`mongodb connected :${conn.connection.host}`);
12 } catch (error) {
13 console.error(`Error:${error.message}`);
14 process.exit();
15 }
16};
17
18module.exports = connectDB;
19
20
1Extra clever
2Earthbound spirit
3Ghost in the form
4Of a mongoose
5
6And I have hands
7And I have feet
8I'll never die
9I am a freak
10
11Hello, I'm here
12I'm living in the wall
13I know I might be small, but
14I, I, I, am a
15Freak
16
17Thou wilt never
18Know what I am
19I am the fifth dimension
20And I'll split the atom
21
22And if you see me
23You're paralyzed
24Pillar of salt
25You're mummified
26
27Hello, I'm here
28I'm living in the wall
29I know, I might be small
30But I am the Eighth Wonder
31
32And I was born
331852
34And I was born
35In India
36
37And I shall haunt
38Like the Buggane
39With such weird noise
40And clanking chains
41
42Hello, I'm here
43I'm living in the wall
44I know I might be small, but
45I, I, I, am a
46Freak
47
48I say "vanished"
49To underground
50Jim, let me go
51I watch like Hell
52
53And I have hands
54And I have feet
55I'll never die
56I am a freak
57
58Hello, I'm here
59I'm living in the wall
60I know I might be small
61But I am the eighth wonder
62
63Eighth wonder of the world
64You'll never get to see
65What in the name of God can I be?
66