MongoDB
 sql >> Datenbank >  >> NoSQL >> MongoDB

Mongodb-Typänderung in Array

Die obige Antwort von Nitin Garg funktioniert fast, außer dass sein Beispiel von einer Zeichenfolge in einen Hash konvertiert wird, NICHT von einer Zeichenfolge in ein Array.

Unter Berücksichtigung der Kommentare von Joel Harris würde die richtige Lösung wie folgt aussehen:

db.jobs.find( { "jobLocationCity" : { $type : 2 } } ).snapshot().forEach( function (x) {
    x.jobLocationCity = [ jobLocationCity ];
    db.jobs.save(x);
});

Oder bei Verwendung von db.eval:

function f() {
    db.jobs.find( { "jobLocationCity" : { $type : 2 } } ).snapshot().forEach( function (x) {
        x.jobLocationCity = [ jobLocationCity ];
        db.jobs.save(x);
    });
}
db.eval(f);