-
Notifications
You must be signed in to change notification settings - Fork 1
/
test1.js
81 lines (61 loc) · 1.92 KB
/
test1.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
'use strict';
const mongo = require('mongodb');
async function sleep() {
return new Promise((resolve) => {
setTimeout(resolve, 1000);
});
}
(async () => {
const db = await mongo.connect('mongodb://localhost:27017/avito');
const task2 = db.collection('task2');
const task3 = db.collection('task3');
// let res1 = await task2.insertOne({foo: 1});
// let res2 = await task2.insertOne({foo: 2});
// let res3 = await task3.insertOne({statusObject: res1.statusObject});
// await task3.updateOne({}, {$set: {foo: 2}});
console.log(typeof insertedId);
let advertisement;
while (true) {
let cursor;
const statusObject = await task3.findOne();
if (statusObject === null) {
// first run
await task3.insertOne({insertedId: 0});
cursor = await task2.find();
} else if (statusObject.insertedId === 0) {
cursor = await task2.find();
} else {
cursor = await task2.find({_id: {$gt: statusObject.insertedId}});
}
while ((advertisement = await cursor.next()) !== null) {
console.log(advertisement);
await sleep();
await task3.updateOne({}, {$set: {insertedId: advertisement._id}});
}
await task3.updateOne({}, {$set: {insertedId: 0}});
}
// setTimeout(() => {
// res4.next().then(function (next) {
// console.log(next);
// });
// }, 5000);
//
// setTimeout(() => {
// res4.next().then(function (next) {
// console.log(next);
// });
// }, 10000);
//
// setTimeout(() => {
// res4.next().then(function (next) {
// console.log(next);
// });
// }, 12000);
//
// setTimeout(() => {
// res4.next().then(function (next) {
// console.log(next);
// });
// }, 15000);
await db.close();
})();