-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updating records with deleted documents
- Loading branch information
1 parent
1fd5c3e
commit 9de85c2
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
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,8 @@ | ||
{ | ||
"defaultEnv": "local", | ||
"local": { | ||
"driver": "mongodb", | ||
"database": "dbname", | ||
"host": "localhost" | ||
} | ||
} |
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,75 @@ | ||
'use strict'; | ||
|
||
var dbm; | ||
var type; | ||
var seed; | ||
|
||
/** | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function(options, seedLink) { | ||
dbm = options.dbmigrate; | ||
type = dbm.dataType; | ||
seed = seedLink; | ||
}; | ||
|
||
exports.up = async function(db) { | ||
console.log('**** Updating Records with Deleted Attachments ****'); | ||
|
||
// Collection Names | ||
const collectionName = 'redacted_record_subset'; | ||
const nrptiCollectionName = 'nrpti'; | ||
|
||
const mClient = await db.connection.connect(db.connectionString, { | ||
native_parser: true | ||
}); | ||
|
||
|
||
const collection = await mClient.collection(collectionName); | ||
const nrptiCollection = await mClient.collection(nrptiCollectionName); | ||
|
||
// Subquery to find _id values with schema 'Document' in both collections | ||
const subquery = [ | ||
{ _schemaName: 'Document' }, | ||
{ _schemaName: 'Document' } | ||
]; | ||
|
||
Promise.all([ | ||
collection.find({ $or: subquery }, { _id: 1 }).toArray(), | ||
nrptiCollection.find({ $or: subquery }, { _id: 1 }).toArray() | ||
]) | ||
.then(([collectionResults, nrptiResults]) => { | ||
// Extract _id values from the subquery results | ||
const validIds = [...new Set([...collectionResults, ...nrptiResults].map(item => item._id))]; | ||
|
||
// Update documents to an empty array where _id is not in validIds | ||
const updateQuery = { | ||
_id: { $nin: validIds } | ||
}; | ||
|
||
const updateOperation = { | ||
$set: { | ||
documents: [] | ||
} | ||
}; | ||
|
||
return collection.updateMany(updateQuery, updateOperation); | ||
}) | ||
.then(updateResult => { | ||
console.log(`Updated ${updateResult.modifiedCount} documents in the ${collectionName} collection.`); | ||
}) | ||
.catch(err => { | ||
}); | ||
|
||
mClient.close(); | ||
|
||
} | ||
|
||
exports.down = function(db) { | ||
return null; | ||
}; | ||
|
||
exports._meta = { | ||
"version": 1 | ||
}; |