From a4207a81d5dea267d64156b37fa14ed0a1af0e50 Mon Sep 17 00:00:00 2001 From: Kar Rui Lau Date: Thu, 17 Jun 2021 11:16:46 +0800 Subject: [PATCH] docs(script): add script to remove permissionList.read key from the db (#2190) --- .../remove-permissionList-read.js | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 scripts/20210616_remove-permissionList-read/remove-permissionList-read.js diff --git a/scripts/20210616_remove-permissionList-read/remove-permissionList-read.js b/scripts/20210616_remove-permissionList-read/remove-permissionList-read.js new file mode 100644 index 0000000000..ef2b5ebd02 --- /dev/null +++ b/scripts/20210616_remove-permissionList-read/remove-permissionList-read.js @@ -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) +} \ No newline at end of file