mongodbazurepowershellazure-devopsazure-cosmosdb-mongoapi

Can Azure release pipeline task inline Powershell connecting to MongoDB load(file) method


Been working with MDBC powershell module to connect to MongoDB instance in Azure cosmosDB. Need to load a .js file with the code to update the collections..

Example:(mikesSuperHappyJS.js)

var collection = 'MikesContext';

   use('HappyMongoDBStuff);
   db[collection].drop();
   db.createCollection(collection);
   db[collection].createIndex({ MarketId: 1 });

db[collection].insert({
"_id": "9875309",
"_t": "MikesContext",
"MikesId": 98457,
"MikesName": "Mike",
"UWGuideLink": "https://mikedopp.com/funnyman.pdf",
"KnowledgeBaseLink": ""
});

would love to use

 MongoDB "load(file)" or Load(mikesSuperHappyJS.js) to import this very important .js file. 

However Im not finding a way to do such a thing. https://www.mongodb.com/docs/v5.0/reference/method/load/

used mdbc to do the powershell module work but cant find a "easy" way to just load the .js file. Any suggestions?


Solution

  • The load() seems is a method in the MongoDB Shell and not available for MDBC PowerShell module.

    To use the load() method in PowerShell, you can try with the following steps:

    1. Download and install MongoDB Shell (mongosh.exe) on the agent machine.

    2. On PowerShell, you first need to run the mongosh command to connect to your MongoDB deployment.

      mongosh "{connection string}" -u {username} -p {password}
      
    3. Then you can run the following command with the load() method to load and execute the .js file.

      load( "path/to/mikesSuperHappyJS.js" )
      

    For more details, you can see "Write Scripts - MongoDB Shell".