In genere non si corregge:se hai la stringa https://aaa/xxx/aaa (yyy è uguale a yyy ) ti ritroverai con https://bbb/xxx/bbb .Ma se sei d'accordo, il codice funzionerà.
Per aggiungere informazioni di debug usa print funzione:
var cursor = db.test.find();
while (cursor.hasNext()) {
var x = cursor.next();
print("Before: "+x['source']['url']);
x['source']['url'] = x['source']['url'].replace('aaa', 'bbb');
print("After: "+x['source']['url']);
db.test.update({_id : x._id}, x);
}
(E comunque, se vuoi stampare oggetti, c'è anche printjson funzione)