-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(script): add script to remove permissionList.read key from the db (
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
scripts/20210616_remove-permissionList-read/remove-permissionList-read.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable */ | ||
|
||
// Number of forms with permissionList.read key - expect 0 after running update | ||
db.forms.count({ | ||
permissionList: { $elemMatch: { read: { $exists: true } } } | ||
}) | ||
|
||
{ | ||
// Number of objects in permissionList with read key - expect 0 after running update | ||
const permissionListWithReadKey = [ | ||
{ | ||
$match: { | ||
permissionList: { $elemMatch: { read: { $exists: true } } } | ||
}, | ||
}, | ||
{ $project: { permissionList: 1 } }, | ||
{ $unwind: '$permissionList' }, | ||
{ $match: { 'permissionList.read': { $exists: true } } }, | ||
{ $count: 'numObjs' } | ||
] | ||
|
||
db.getCollection('forms').aggregate(permissionListWithReadKey) | ||
} | ||
|
||
// Update - Remove read key | ||
db.forms.update( | ||
{ 'permissionList.read': { $exists: true } }, | ||
{ $unset: { 'permissionList.$[elem].read': 1 } }, | ||
{ arrayFilters: [{ 'elem.read': { $exists: true } }], multi: true } | ||
) | ||
|
||
|
||
// Check again, should be 0. | ||
db.forms.count({ | ||
permissionList: { $elemMatch: { read: { $exists: true } } } | ||
}) | ||
|
||
// Check again, should be 0 | ||
{ | ||
// Number of objects in permissionList with read key - expect 0 after running update | ||
const permissionListWithReadKey = [ | ||
{ | ||
$match: { | ||
permissionList: { $elemMatch: { read: { $exists: true } } } | ||
}, | ||
}, | ||
{ $project: { permissionList: 1 } }, | ||
{ $unwind: '$permissionList' }, | ||
{ $match: { 'permissionList.read': { $exists: true } } }, | ||
{ $count: 'numObjs' } | ||
] | ||
|
||
db.getCollection('forms').aggregate(permissionListWithReadKey) | ||
} |