-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-read_gen.js
40 lines (37 loc) · 1.67 KB
/
be-read_gen.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
const mongoose = require('mongoose');
const { readSchema } = require('./schema');
const { mongo_conf } = require('./config')
const conn_finish = mongoose.connect(mongo_conf.url, { useNewUrlParser: true, useUnifiedTopology: true});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'database connection error'));
db.once('open', () => { console.log('connection with database established.'); });
(async() => {
await conn_finish;
const ReadModel = mongoose.model('Read', readSchema);
const pipeline = [
{
$group: {
_id : '$aid',
aid : { $first: '$aid' },
category: { $first: '$category' },
readNum : { $sum: { $toInt: '$readOrNot' }},
commentNum : { $sum: { $toInt: '$commentOrNot' }},
agreeNum : { $sum: { $toInt: '$agreeOrNot' }},
shareNum : { $sum: { $toInt: '$shareOrNot' }},
readUidList : { $addToSet: { $cond: [ {$eq: ['$readOrNot', '1']}, '$uid', '$noval'] }},
commentUidList : { $addToSet: { $cond: [{ $eq: ['$commentOrNot', '1'] }, '$uid', '$noval'] } },
agreeUidList : { $addToSet: { $cond: [{ $eq: ['$agreeOrNot' , '1'] }, '$uid', '$noval'] } },
shareUidList : { $addToSet: { $cond: [{ $eq: ['$shareOrNot' , '1'] }, '$uid', '$noval'] } }
}
},
{ $unset: '_id' },
{ $merge: {
into: 'be-read',
on: ['category', 'aid'],
whenMatched: 'replace',
whenNotMatched: 'insert'
} }
]
await ReadModel.aggregate(pipeline);
await mongoose.disconnect();
})();