NodeJs/ExpressJs--Mongodbデータベースに接続する(添削・修正)
8309 ワード
インストール:
npm install mongodb--save/cnpm install mogodb--save-dev
URL:https://docs.mongodb.com/getting-started/node/introduction/https://docs.mongodb.com/getting-started/node/query/
Mongodbを導入
var Mongo Client=require('mongodb').Mongo Client;
データベースアドレス
let data BaseUrl='mongodb://localhost:27017/データベース名;
ケース:
app.js
npm install mongodb--save/cnpm install mogodb--save-dev
URL:https://docs.mongodb.com/getting-started/node/introduction/https://docs.mongodb.com/getting-started/node/query/
Mongodbを導入
var Mongo Client=require('mongodb').Mongo Client;
データベースアドレス
let data BaseUrl='mongodb://localhost:27017/データベース名;
ケース:
app.js
let express = require('express');
//
let MongoClient = require('mongodb').MongoClient;
let app = express();
// ,
let dataBaseUrl = 'mongodb://localhost:27017/person';
/*
app.get('/',(req,res) => {
// ,
MongoClient.connect(dataBaseUrl,(err,db) => {
res.writeHead(200,{"Content-Type":"text/html;Charset=utf-8"});
if(err) {
res.send(' !')
}else{
res.write(' , !
');
//
db.collection('student').insertOne({'name':"honghong",'age':22},(err,result) => {
if(err) {
res.send(' !');
return;
}else{
res.write(' , !');
res.end();
//
db.close();
}
})
}
});
});
*/
/*
//
app.get('/',(req,res) => {
// ,
MongoClient.connect(dataBaseUrl,(err,client) => {
res.writeHead(200,{"Content-Type":"text/html;Charset=utf-8"});
if(err) {
res.send(' !')
}else{
res.write(' , !
');
//3.0
let db = client.db("person");
db.collection('student').insertOne({'name':"honghong1",'age':22},(err,result) => {
if(err) {
res.send(' !');
return;
}else{
res.write(' , !');
res.end();
//
client.close();
}
})
}
});
});
*/
/*
//
app.get('/',(req,res) => {
MongoClient.connect(dataBaseUrl,(err,client) => {
res.writeHead(200,{"Content-Type":"text/html;Charset=utf-8"});
if(err) {
res.send(' !')
}else{
res.write(' !');
let db = client.db('person');
db.collection('student').deleteMany({'name':'honghong'},(err,result) => {
if(err) {
res.send(' !')
return false;
}else{
console.log(result)
res.write(" !");
res.end();
client.close()
}
})
}
})
})
*/
/*
//
app.get('/',(req,res) => {
res.writeHead(200,{"Content-Type":"text/html;Charset=utf-8"});
MongoClient.connect(dataBaseUrl,(err,client) => {
if(err){
res.send(' !');
}else{
res.write(' !');
let db = client.db('person');
db.collection('student').updateOne({'name':'honghong1'},{$set:{'age':30}},(err,result) => {
if(err){
res.send(' !');
}else{
res.write(' !');
res.end();
client.close()
}
})
}
})
})
*/
/*
//
app.get('/',(req,res) => {
res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
MongoClient.connect(dataBaseUrl,(err,client) => {
if(err) {
res.send(' !')
}else{
res.write(' !');
let db = client.db('person');
db.collection('student').find().toArray((err,result) => {
if(err){
res.send(' !')
}else{
res.write(' !');
console.log(result);
console.log(result instanceof Array);//true
res.end();
client.close()
}
})
}
})
})
*/
/*
//
app.get('/',(req,res) => {
res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
MongoClient.connect(dataBaseUrl,(err,client) => {
if(err){
res.send(' !')
}else{
res.write(' !');
let db = client.db('person');
db.collection('student').find().sort({"age":1}).toArray((err,result) => {
if(err) {
res.send(' !')
}else{
res.write(' !');
console.log(result);
res.end();
client.close();
}
})
}
})
})
*/
/*
//
app.get('/',(req,res) => {
MongoClient.connect(dataBaseUrl,(err,client) => {
res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
if(err) {
res.send(' !')
}else{
res.write(' !');
let db = client.db('person');
db.collection('student').find().limit(5).toArray((err,result) => {
if(err) {
res.send(" !")
}else{
res.write(' !');
console.log(result);
res.end();
client.close()
}
})
}
})
})
*/
/*
//
app.get('/',(req,res) => {
MongoClient.connect(dataBaseUrl,(err,client) => {
res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
if(err) {
res.send(' !')
}else{
res.write(' !');
let db = client.db('person');
db.collection('student').find().skip(2).toArray((err,result) => {
if(err){
res.send(" !")
}else{
res.write(" !");
console.log(result);
res.end();
client.close()
}
})
}
})
})
*/
// $lookup
app.get('/',(req,res) => {
MongoClient.connect(dataBaseUrl,(err,client) => {
res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
if(err) {
res.send(' !');
}else{
res.write(' !');
let db = client.db('person');
db.collection('student').aggregate([
{
$lookup:
{
from:'teacher',// ( teacher )
//localField、foreignField
localField:"neme",// join ( )
foreignField:"neme",// join ( )
as:'studentTeacher'// ( array)
}
}
]).toArray((err,result) => {
if(err) {
res.send(' !')
}else{
res.write(' !');
// json
console.log(JSON.stringify(result));
res.end();
client.close()
}
})
}
})
})
app.listen(8002)