Followup: Aggregate multiple databases (one DB is placed locally on PC, second - on host in net)

Follow up on Aggregate multiple databases (one DB is placed locally on PC, second - on host in net) - #4 by Andrei by @Andrei

Some incomplete js code for you scenario.

const local_database_name = ... ;
const local_collection_name = ... ;
const local_client = new MongoClient( local_uri ) ;
const local_database = client.db( local_database_name ) ;
const local_collection = database.collection( local_collection_name ) ;
const local_query = { ... }
const local_documents = local_collection.find( local_query ).toArray() ;

const net_database_name = ... ;
const net_collection_name = ... ;
const net_client = new MongoClient( net_uri ) ;
const net_database = client.db( net_database_name ) ;
const net_collection = database.collection( net_collection_name ) ;
const net_query = { ... }
const net_documents = net_collection.find( net_query ).toArray() ;

/* magic_function is the function that compares metadata and do what ever it needs
   to update the local_documents from the net_documents
*/
const modified_documents = magic_function( local_documents , net_documents ) ;

const temp_collection_name = "_temp_collection" ;
const temp_collection = local_database.collection( temp_collection_name ) ;

/* insert the modified documents into the temporary collection
*/
temp_collection.insertMany( modified_documents ) ;

/* merge modified documents into the original local collection
*/
temp_collection.aggregate( { "$merge" : {
  "into" : local_collection_name ,
  "on" : _id ,
  "whenMatched" : "merge" ,
  "whenNotMatched" : "discard" } } )

You might need some await here and there but I am not fluent enough with JS to know exactly where it is needed.

You might want to use different values for whenMatched and whenNotMatched.

3 Likes

Merci bien vôtre aide!

1 Like