Skip to content

Commit

Permalink
updating records with deleted documents
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjaytkbabu committed Sep 1, 2023
1 parent 1fd5c3e commit 9de85c2
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/database.json-sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"defaultEnv": "local",
"local": {
"driver": "mongodb",
"database": "dbname",
"host": "localhost"
}
}
75 changes: 75 additions & 0 deletions api/migrations/20230901215837-updateDeletedDocRecords.js
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
};

0 comments on commit 9de85c2

Please sign in to comment.