-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmigrate-schedules-array.js
More file actions
62 lines (61 loc) · 1.75 KB
/
migrate-schedules-array.js
File metadata and controls
62 lines (61 loc) · 1.75 KB
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
db.getCollection('Schedule').aggregate([
{
$match: {
status: 1,
contentStatus: 1,
published: { $exists: true },
section: { $exists: true },
option: { $exists: true },
'content.$id': { $exists: true },
},
},
{ $addFields: { contentArray: { $objectToArray: '$content' } } },
{ $unwind: '$contentArray' },
{ $match: { 'contentArray.k': '$id' } },
{
$project: {
contentId: '$contentArray.v',
contentType: '$content.type',
hasImage: { $ifNull: ['$hasPrimaryImage', false] },
sectionId: '$section',
optionId: '$option',
start: {
$cond: {
if: { $gt: ['$startDate', '$published'] },
then: '$startDate',
else: '$published',
},
},
end: {
$cond: {
if: { $lt: [{ $ifNull: ['$endDate', ISODate('2038-01-01T00:00:00')] }, { $ifNull: ['$expires', ISODate('2038-01-01T00:00:00')] }] },
then: '$endDate',
else: '$expires',
},
},
},
},
{ $sort: { start: -1 } },
{
$group: {
_id: '$contentId',
contentType: { $first: '$contentType' },
hasImage: { $first: '$hasImage' },
schedules: { $push: { sectionId: '$sectionId', optionId: '$optionId', start: '$start', end: '$end' } },
},
},
{
$project: {
_id: 0,
contentId: '$_id',
contentType: 1,
hasImage: 1,
created: ISODate(),
schedules: 1,
},
},
{ $out: 'SectionQuery' },
], { allowDiskUse: true });
db.getCollection('SectionQuery').createIndex({ 'schedules.sectionId' : 1, 'schedules.optionId' : 1 });
db.getCollection('SectionQuery').createIndex({ contentId: 1 }, { unique: true });
db.getCollection('SectionQuery').createIndex({ 'schedules.0.start' : -1, _id: -1 });