Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ Algorithm Library started in this project
## May-7 (Tue), 2019
Decided to start the project
* go.storeformula.com

## Markup
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
1 change: 1 addition & 0 deletions connect-database/connect-db.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// How to run : $ node .\connect-db.js
// make sure if port is correct (windows=5433, mac=5432)
var Pool = require('pg').Pool;
var config = {
host: 'localhost',
Expand Down
21 changes: 21 additions & 0 deletions connect-database/connect-db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,24 @@ insert into students(firstname,lastname, country) values
('Simon','Park', 'South Korea'),
('Cindy','Kwon', 'North Korea')
;


/************************************
4) basic command after connection (In Mac)
************************************/

Simons-Air:bin simonpark$ psql -U postgres -p 5432
password: connect

-- list DB
postgres-# \l

-- connect DB
postgres-# \c {school}

-- list tables
school-# \dt

-- quit DB
school-# \q

8 changes: 5 additions & 3 deletions connect-service/cis_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const express = require('express'),

const morgan = require('morgan')
const router = require('./db_query');
const bodyParser = require('body-parser')
// const bodyParser = require('body-parser')

app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
Expand All @@ -17,8 +17,10 @@ app.use(function(req, res, next) {
});

app.use(morgan('combined')) // combined , short
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({ extended: true }));
// app.use(bodyParser.json());
app.use(express.urlencoded({extended:false}))
app.use(express.json())
app.use(router)


Expand Down
3 changes: 2 additions & 1 deletion connect-service/db_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ const express = require('express')
var pool = require('pg').Pool;
const router = express.Router()

// make sure if port is correct (windows=5433, mac=5432)
function getConnection(){
return pool({
host: 'localhost',
user: 'mycampus',
password: 'mycampus',
database: 'school',
port: '5433',
port: '5432',
connectionList: 10

})
Expand Down
62 changes: 22 additions & 40 deletions database/mongo/mongo.js → database/mongo/mongo-command.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,35 @@
// https://www.tutorialspoint.com/mongodb/

// 1) Show DB Systems
//show dbs
[mong DB manual](https://docs.mongodb.com/manual/reference/method/db.createUser/)

// admin 0.000GB
// config 0.000GB
// local 0.000GB
// node-mongo-registration-login-api 0.000GB
1) Create Collection

// 2) Switch to the DB
//use mycustomers

// switched to db mycustomers
db
// mycustomers

// 3) Create User
// db.createUser({
// ... user:"simon",
// ... pwd:"1234",
// ... roles: [ "readWrite", "dbAdmin" ]
// ... });
// https://docs.mongodb.com/manual/reference/method/db.createUser/

// Successfully added user: { "user" : "simon", "roles" : [ "readWrite", "dbAdmin" ] }

// 4) Create Collection
```
db.createCollection('customers');
// { "ok" : 1 }
```

// 5) Show Collections
// show collections

// customers
2) Show Collections

```
db.customers.insert({first_name:"John",last_name:"Doe"});
// WriteResult({ "nInserted" : 1 })
```

3) search Document
```
db.customers.find();
// { "_id" : ObjectId("5c06ec2a49f099d2326ecad1"), "first_name" : "John", "last_name" : "Doe" }
```

db.customers.find();
// { "_id" : ObjectId("5c06ec2a49f099d2326ecad1"), "first_name" : "John", "last_name" : "Doe" }

//show collections
// customers
// people
```
db.people.find();
// { "_id" : ObjectId("5c06ec582a88942510eb20ae"), "firstname" : "John", "lastname" : "Doe", "address" : "330 Bay St. Toronto", "__v" : 0 }
// { "_id" : ObjectId("5c06ec582a88942510eb20af"), "firstname" : "Jane", "lastname" : "Doe", "address" : "555 Main St.", "__v" : 0 }
```
4) insert Document
```
db.customers.insert([{first_name:"Steve", last_name:"Simth"},{first_name:"John",last_name:"Johnson",gender:"female"}]);

// BulkWriteResult({
// "writeErrors" : [ ],
// "writeConcernErrors" : [ ],
Expand All @@ -60,10 +40,13 @@ db.customers.insert([{first_name:"Steve", last_name:"Simth"},{first_name:"John",
// "nRemoved" : 0,
// "upserted" : [ ]
// })

```

5) Examples
```

db.customers.find();
// { "_id" : ObjectId("5c06ec2a49f099d2326ecad1"), "first_name" : "John", "last_name" : "Doe" }
// { "_id" : ObjectId("5c06ed5749f099d2326ecad2"), "first_name" : "Steve", "last_name" : "Simth" }
// { "_id" : ObjectId("5c06ed5749f099d2326ecad3"), "first_name" : "John", "last_name" : "Johnson", "gender" : "female" }
db.customers.find().pretty();
db.customers.find().sort({last_name:1}); // ascending
db.customers.find().sort({last_name:-1}); // descending
Expand Down Expand Up @@ -91,5 +74,4 @@ db.customers.find({first_name:"Steve"});
db.customers.find({$or:[{first_name:"Steve"},{last_name:"Doe"}]});

db.customers.find({"address.city":"Boston"});


```
39 changes: 39 additions & 0 deletions database/mongo/mongo-general.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# start mongo DB

1) start deamon
```
$ cd ~/Documents/dev/mongodb-4.0/bin
$ ./mongod
```
2) start mongo
```
$ ./mongo
> show dbs #
> use {db_name} #
> show collections #
> db.{collection_name}.find() #

```

3) Create User
```sql
db.createUser({
user:"simon",
pwd:"1234",
roles: [ "readWrite", "dbAdmin" ]
});

```

4) MongoDB Compass

```
mongodb://localhost:27017
```

# reference
[mongo DB manual](https://docs.mongodb.com/manual/reference/method/db.createUser/)

[mongoose](https://mongoosejs.com/)

[Tutorial Point](https://www.tutorialspoint.com/mongodb/mongodb_delete_document.htm)
81 changes: 81 additions & 0 deletions database/mongo/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// how to run : nodemon server.js
// Node.js (Mongo DB) Tutorial # 9 / 10
// https://www.youtube.com/watch?v=bxsemcrY4gQ
// https://www.youtube.com/watch?v=VVGgacjzc2Y

const mongoose = require('mongoose');

var express = require('express'),
app = express(),
port = process.env.PORT || 8080;

// 1) DB connect
mongoose.connect('mongodb://localhost/cis',{
useNewUrlParser: true,
useUnifiedTopology: true
})

mongoose.connection.on('connected', () => {
console.log('Mongo DB connected')
});

// 2) DB schema
const studentSchema = new mongoose.Schema({
firstName: String,
lastName: String,
staff: String,
})


// 3) DB model
const studentModel = mongoose.model('student',studentSchema);

app.use(express.json());
app.use(express.urlencoded({extended:false}));

app.get('/students', (req, res) => {
try{
console.log('main workflow:');
studentModel.find()
.then((result) => {
console.log(result);
res.send(result);
})
.catch((err) => console.log(err));
} catch(err) {
console.log('Internal Error:', error);
}
//Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
// res.json()
});

app.post('/add', (req, res) => {
const data = {
firstName: req.body.firstName,
lastName: req.body.lastName,
staff: req.body.staff
};
const newStudent = new studentModel(data);

try {
newStudent.save((error) => {
if (error) {
res.status(500).json({ msg: 'Sorry, internal server errors' });
return;
}
console.log('your data has been saved');
return res.json({
msg: 'Your data has been saved!!!!!!'
});
});

} catch(err){
console.error(err.message);
}
});

// Run service
app.listen(port, () => {
console.log('Mongo DB started on: ' + port);
});

59 changes: 59 additions & 0 deletions database/mongo/version1/server1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// how to run : nodemon server.js

const express = require('express');
const mongoose = require('mongoose');

const app = express();
const port = process.env.PORT || 8080;

mongoose.connect('mongodb://localhost/mern',{
useNewUrlParser: true,
useUnifiedTopology: true
})

mongoose.connection.on('connected', () => {
console.log('Mongo DB connected')
});

// schema
const schema = mongoose.Schema;
const blogSchema = new mongoose.Schema({
title: String,
body: String,
date: {
type: String,
default: Date.now()
}
})


// model
const blogPost = mongoose.model('blogPost',blogSchema);

const data = {
title: "Welcome",
body: "Help folks.."
};

const newBlogPost = new blogPost(data);
newBlogPost.save((error) => {
if(error) {
console.log('error');
} else {
console.log('saved')
}
});


app.get('/api', (req, res) => {
const data = {
username: 'simonpark',
age: 5
};
res.json(data);
})

app.listen(port, () => {
console.log('Mongo DB started on: ' + port);
});

Loading