-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_process.js
169 lines (143 loc) · 4.43 KB
/
db_process.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// 数据表 处理
var mongoose = require('./model/db');
var Article_Model = mongoose.model('Article'); // 使用User模型
var ArticleDetail = mongoose.model('ArticleDetail');
var cheerio = require('cheerio');
function updatePageOnArticle(pageNO, toPage, count) {
for (var i = 0; i < count; i++) {
// Article_Model.findOneAndUpdate({page: pageNO}, {page: toPage})
Article_Model.findOneAndUpdate({"page": pageNO}, {"page":toPage }, function (err, doc) {
if (err) {
console.log(err)
return;
}
console.log('success...');
})
// db.getCollection("Articles").update({"page":pageNO},{"$set":{"page":toPage}}, false, false)
// db.getCollection('Article').update({}, {$rename : {"abstract" : "_abstract"}}, false, true)
}
}
// 批量插入新字段
// db.getCollection("articles").update({"page":{$exists:false}},{"$set":{"page":1}},false,true)
function updateAllPageOnArticle(pageSize) {
var total = Article_Model.count(function (err, num) {
if (err) {
console.log('failed...')
return 0;
}
return num;
});
if (total !== 0) {
var piece = total / pageSize;
for (var toPage = piece; toPage > 0; toPage--) {
for (var i = 0; i < pageSize; i++) {
Article_Model.findOneAndUpdate({"page": 1}, {"page": toPage}, function (err, doc) {
if (err) {
console.log(err)
return;
}
console.log('success...');
})
}
}
}
}
function countArtical() {
Article_Model.count(function (err, num) {
if (err)
console.log('failed...')
else {
console.log('count:' + num);
}
});
}
function countArticalDetail() {
ArticleDetail.count(function (err, num) {
if (err)
console.log('failed...')
else {
console.log('count:' + num);
}
});
}
function queryArticleDetailByAuthor(author) {
ArticleDetail.findOne({'author': author}, function (err, item) {
if (err)
console.log('failed...');
else
console.log('result: ' + item);
})
}
async function queryArticleDetailByhref(href) {
var txt = '';
await ArticleDetail.findOne({'href': href}, function (err, item) {
if (err) {
console.log('failed...');
}
else {
console.log('result: ' + item);
txt = item.text;
}
})
console.log(content);
}
// 根据 href字段 查询单条信息
function queryByhref(href) {
Article_Model.findOne({"href": href}, (err, item) => {
if (err)
console.log('failed...')
else
console.log('result: ' + item)
})
}
function clearDatabase() {
// 移除整个collection数据表
ArticleDetail.remove({}, function (err) {
if (err)
console.log(err);
else
console.log('drop collection ArticleDetail success...');
});
Article_Model.remove({}, function (err) {
if (err)
console.log(err);
else
console.log('drop collection Article_Model success...');
});
}
function removeByAuthor(str) {
if (!str)
return;
else
var author = str;
var condition = {
$or: [{"author": author}] // 条件满足其一
// $and: [{"username": usr}, {"goodAt": good}] // 同时满足条件
}
Article_Model.findOneAndRemove({"author": author}, function (err, doc) {
if (err) {
console.log(err)
return;
}
console.log('remove success...');
})
}
queryArticleDetailByhref('/p/30248d503ca1');
// updatePageOnArticle(1, 2, 20);
// updateAllPageOnArticle(20);
// clearDatabase();
// 查看 db.articles.find()
// 删除collections CLI: db.articles.drop()
// removeByAuthor('阿阳sunny');
// queryDatabase();
// countArtical();
// countArticalDetail();
// queryByhref('/p/d315a19a991a');
// queryArticleDetailByAuthor('潇逸霏');
// queryArticleDetailByhref('/p/c5bc31531ecb');
// updateCollections()
function updateCollections() {
// Article collection and update "abstract" column
db.getCollection('Article').update({}, {$rename : {"abstract" : "_abstract"}}, false, true)
// Schema.findOneAndUpdate({_id: id},{test: “更新字段”}).exex();
}