Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

383
Views
Running mongo shell script using kubernetes job

I need to create kubernetes job that will run below script on mongo shell:

var operations = [];
db.product.find().forEach(function(doc) {
    var documentLink = doc.documentLink; 
    var operation = { updateMany :{ 
"filter" : {"_id" : doc._id},
"update" : {$set:{"documentLinkMap.en":documentLink,"documentLinkMap.de":""},
    $unset: {documentLink:"","descriptionMap.tr":"","news.tr":"","descriptionInternal.tr":"","salesDescription.tr":"","salesInternal.tr":"","deliveryDescription.tr":"","deliveryInternal.tr":"","productRoadMapDescription.tr":"","productRoadMapInternal.tr":"","technicalsAndIntegration.tr":"","technicalsAndIntegrationInternal.tr":"","versions.$[].descriptionMap.tr":"","versions.$[].releaseNoteMap.tr":"","versions.$[].artifacts.$[].descriptionMap.tr":"","versions.$[].artifacts.$[].artifactNotes.tr":""}}}};
    operations.push(operation); 
});
operations.push( {
    ordered: true,      
    writeConcern: { w: "majority", wtimeout: 5000 } 
});
db.product.bulkWrite(operations);

I will need a sample of how that job will look like. Should I create presistent volume and claim to it or is there possibility to run this job without persistent volume? I need to run this once and then remove it.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can solve it much easier with configMap and then mount the configMap as a volume which will be resolved in a file.

Below is example how to proceed with it (Note! You will need to use a proper image for it as well as some other changes how mongo shell works):

  1. Create a configMap from file. Can be done by running this command:

    $ kubectl create cm mongoscript-cm --from-file=mongoscript.js
    configmap/mongoscript-cm created
    

    You can check that you file is stored inside by running:

    $ kubectl describe cm mongoscript-cm
    
  2. Create a job with volume mount from configmap (spec template is the same as it used in pods):

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: mongojob
    spec:
      template:
        spec:
          containers:
          - name: mongojob
            image: ubuntu # for testing purposes, you need to use appropriate one
            command: ['bin/bash', '-c', 'echo STARTED ; cat /opt/mongoscript.js ; sleep 120 ; echo FINISHED'] # same for command, that's for demo purposes
            volumeMounts:
            - name: mongoscript
              mountPath: /opt # where to mount the file
          volumes:
          - name: mongoscript
            configMap:
              name: mongoscript-cm # reference to previously created configmap
          restartPolicy: OnFailure # required for jobs
    
  3. Checking how it looks inside the pod

    Connect to the pod:

    $ kubectl exec -it mongojob--1-8w4ml -- /bin/bash
    

    Check file is presented:

    # ls /opt
    mongoscript.js
    

    Check its content:

    # cat /opt/mongoscript.js 
    var operations = [];
    db.product.find().forEach(function(doc) {
        var documentLink = doc.documentLink; 
        var operation = { updateMany :{ 
    "filter" : {"_id" : doc._id},
    "update" : {$set {"documentLinkMap.en":documentLink,"documentLinkMap.de":""},
        $unset: {documentLink:"","descriptionMap.tr":"","news.tr":"","descriptionInternal.tr":"","salesDescription.tr":"","salesInternal.tr":"","deliveryDescription.tr":"","deliveryInternal.tr":"","productRoadMapDescription.tr":"","productRoadMapInternal.tr":"","technicalsAndIntegration.tr":"","technicalsAndIntegrationInternal.tr":"","versions.$[].descriptionMap.tr":"","versions.$[].releaseNoteMap.tr":"","versions.$[].artifacts.$[].descriptionMap.tr":"","versions.$[].artifacts.$[].artifactNotes.tr":""}}}};
        operations.push(operation); 
    });
    operations.push( {
        ordered: true,      
        writeConcern: { w: "majority", wtimeout: 5000 } 
    });
    db.product.bulkWrite(operations);
    
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!