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 mand in mongo console, it gives an error : 2015-11-28T12:40:08.342+0530 ReferenceError: $1 is not defined
Any help is greatly appreciated
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 mand in mongo console, it gives an error : 2015-11-28T12:40:08.342+0530 ReferenceError: $1 is not defined
Any help is greatly appreciated
Share Improve this question edited Nov 28, 2015 at 8:24 Sede 61.3k20 gold badges158 silver badges161 bronze badges asked Nov 28, 2015 at 6:43 gehlotpareshgehlotparesh 1423 silver badges14 bronze badges 6- 2 Check This – Narendrasingh Sisodia 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 – gehlotparesh Commented Nov 28, 2015 at 6:55
- @user2767817 your regex is wrong it will not work in Mongo. use the one provided by Uchiha. – Sede 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 – gehlotparesh Commented Nov 28, 2015 at 7:15
- please edit your question to include the new code – Sede Commented Nov 28, 2015 at 7:19
2 Answers
Reset to default 4Your 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 } } );
}
- Regex provided by @Uchiha.
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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744222743a4563861.html
评论列表(0条)