Skip to content

Commit

Permalink
update records
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjaytkbabu committed Sep 6, 2023
1 parent 9de85c2 commit af1f999
Showing 1 changed file with 61 additions and 29 deletions.
90 changes: 61 additions & 29 deletions api/migrations/20230901215837-updateDeletedDocRecords.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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.
Expand All @@ -14,55 +15,86 @@ exports.setup = function(options, seedLink) {
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
});
try {
// Collection Names
const redactedCollectionName = 'redacted_record_subset';
const nrptiCollectionName = 'nrpti';




const collection = await mClient.collection(collectionName);
const redactedCollection = await mClient.collection(redactedCollectionName);
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]) => {
const collectionResults = await redactedCollection.find({ $or: subquery }, { _id: 1 }).toArray();

const nrptiResults = await nrptiCollection.find({ $or: subquery }, { _id: 1 }).toArray();



console.log('inthen>>>>>')
// Extract _id values from the subquery results
const validIds = [...new Set([...collectionResults, ...nrptiResults].map(item => item._id))];
const validIds = [...new Set([...collectionResults, ...nrptiResults].map(item => item._id))];

let redactedDocumentsIds = await redactedCollection.find({_schemaName: 'Document'}).toArray();
redactedDocumentsIds=redactedDocumentsIds.map(item => item._id);

console.log('validIds= ' + validIds.length)

// Update documents to an empty array where _id is not in validIds
const updateQuery = {
_id: { $nin: validIds }
};

const cursor = redactedCollection.find({
"documents": { "$exists": true, "$not": { "$size": 0 } }
}); // You can specify a filter to narrow down the documents

const updateOperation = {
$set: {
documents: []

console.log('before_cursor')
let ct = 0;
await cursor.forEach(record => {
if(!redactedDocumentsIds.includes(record['documents'][0]))
{
console.log('in_if' + ct)
redactedCollection.updateOne(
{_id: record._id},
{$set: {documents: []}}
)
}
};

return collection.updateMany(updateQuery, updateOperation);
})
.then(updateResult => {
console.log(`Updated ${updateResult.modifiedCount} documents in the ${collectionName} collection.`);
})
.catch(err => {
});
else{
console.log('in_else' + ct)
}

ct++;


},
() => {
console.log('in_completion')
// This is the completion callback, called when the iteration is complete
mClient.close(); // Close the MongoDB client connection
console.log('Done.');
}
);

mClient.close();

//mClient.close();
} catch (err) {
console.log('Error:', err);
}

// return null;

}

Expand Down

0 comments on commit af1f999

Please sign in to comment.