4

My table name is : table_video My field name in db is : url_value

Whose value is : http://192.168.1.124/test/abcd/abcd.m3u8

Value which is needed by me : http://192.168.1.124/test/abcd_NEW/abcd_NEW.m3u8

There are multiple values in place of "abcd", above url is just one example.

var cursor = db.table_video.find();
while (cursor.hasNext()) {
  var x = cursor.next();
  print("\n\n-----------------------------------");

  print("Before : url_value : "+x['url_value']);

  x['url_value'] = x['url_value'].replace(/^(.*?)\/test\/(.*?)\/(.*?)\.m3u8$/g, $1/test/$2_NEW/$2_NEW.m3u8);

  print("After : url_value : "+x['url_value']);

  db.table_video.update({_id : x._id}, x);

}

When I execute above command in mongo console, it gives an error : 2015-11-28T12:40:08.342+0530 ReferenceError: $1 is not defined

Any help is greatly appreciated

6
  • 2
    Check This Commented Nov 28, 2015 at 6:49
  • My regex is already working in Kate, even I checked the link you provided, however it's giving error in mongodb Commented Nov 28, 2015 at 6:55
  • @user2767817 your regex is wrong it will not work in Mongo. use the one provided by Uchiha. Commented Nov 28, 2015 at 7:03
  • Regex provided by Uchiha is right, however, when I use in mongodb, it gives error : 2015-11-28T12:40:08.342+0530 ReferenceError: $1 is not defined Commented Nov 28, 2015 at 7:15
  • please edit your question to include the new code Commented Nov 28, 2015 at 7:19

2 Answers 2

4

Your regex is wrong you can get the expected result using this regex1.

But the best way to do this is using "bulk" operations for maximum efficiency.

var bulk = db.table_video.initializeUnorderedBulkOp();
var count = 0;
db.table_video.find().forEach(function(doc) {
    var newUrlValue = doc.url_value.replace(/^(.*?)\/test\/(.*?)\/(.*?)\.m3u8$/, '$1/test/$2_NEW/$3_NEW.m3u8');
    bulk.find( { '_id': doc._id } ).updateOne( { 
        '$set': { 'url_value': newUrlValue } 
    });
    count++;
    if (count % 100 === 0) {
        // Execute per 100 operation and re-init
        bulk.execute();
        bulk = db.table_video.initializeUnorderedBulkOp();
        count = 0;
    }
})

// Clean up queues
if (count > 0)  bulk.execute();

If your MongoDB version is older that 2.6 you need to use a while loop.

var cursor = db.table_video.find();
while (cursor.hasNext()) {
    var x = cursor.next();
    print("\n\n-----------------------------------");
    print("Before : url_value : "+x['url_value']);
    var newUrlValue = x['url_value'].replace(/^(.*?)\/test\/(.*?)\/(.*?)\.m3u8$/, '$1/test/$2_NEW/$2_NEW.m3u8');
    print("After : url_value : "+newUrlValue);
    db.table_video.update({ _id : x._id }, { '$set': { 'url_value': newUrlValue } } );

}

  1. Regex provided by @Uchiha.
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @zypA13510, thanks for pointing this out. Feel free to edit the answer. I will approve it. Use your comment as edit summary.
@zypA13510 I overwrote the decision
1

in mongodb version 4.2 you have regexFind project operator which can be used to get the matches, then you ocan use substr to replace parts of the pattern

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.