GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — JavaScriptStudy/17-nodejs/19-MongoDB.js at master · LCLucky/JavaScriptStudy

Expand file tree

/

19-MongoDB.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
107 lines (97 loc) · 2.53 KB
/

19-MongoDB.js

Copy path

File metadata and controls

107 lines (97 loc) · 2.53 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// 【 1 安装 mongodb 】
// Homebrew安装mongodb
// $ brew update
// $ brew install mongodb
// 【 2 安装驱动 】
// $ cnpm install mongodb
// 【 3 数据库操作( CURD ) 】
// 与 MySQL 不同,MongoDB 会自动创建数据库和集合,使用前不需要手动创建
// 连接数据库 csxiaoyao
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/csxiaoyao';
// 插入数据
// 插入两条数据
var insertData = function(db, callback) {
//连接到表 site
var collection = db.collection('site');
//插入数据
var data = [{"name":"sunshine","url":"www.csxiaoyao.com"},{"name":"csxiaoyao","url":"blog.csxiaoyao.com"}];
collection.insert(data, function(err, result) {
if(err){
console.log('Error:'+ err);
return;
}
callback(result);
});
}
// 查询数据
// 检索 name 为 "csxiaoyao" 的数据
var selectData = function(db, callback) {
//连接到表
var collection = db.collection('site');
//查询数据
var whereStr = {"name":'csxiaoyao'};
collection.find(whereStr).toArray(function(err, result) {
if(err){
console.log('Error:'+ err);
return;
}
callback(result);
});
}
// 更新数据
// 将 name 为 "csxiaoyao" 的 url 改为 "https://www.csxiaoyao.com"
var updateData = function(db, callback) {
//连接到表
var collection = db.collection('site');
//更新数据
var whereStr = {"name":'csxiaoyao'};
var updateStr = {$set: { "url" : "https://www.csxiaoyao.com" }};
collection.update(whereStr, updateStr, function(err, result) {
if(err){
console.log('Error:'+ err);
return;
}
callback(result);
});
}
// 删除数据
// 将 name 为 "csxiaoyao" 的数据删除
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/csxiaoyao';
var delData = function(db, callback) {
//连接到表
var collection = db.collection('site');
//删除数据
var whereStr = {"name":'csxiaoyao'};
collection.remove(whereStr, function(err, result) {
if(err){
console.log('Error:'+ err);
return;
}
callback(result);
});
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
console.log("连接成功");
// // 插入数据
insertData(db, function(result) {
console.log(result);
db.close();
});
// 查询数据
selectData(db, function(result) {
console.log(result);
db.close();
});
// // 更新数据
updateData(db, function(result) {
console.log(result);
db.close();
});
// 删除数据
delData(db, function(result) {
console.log(result);
db.close();
});
});
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.