"how to convert string to array in mongodb?" Code Answer

3

you could try this:

db.collection.find().foreach( function (x) {   
    lines = x.address.split(",");
    obj = {};
    useraddresslist = [];
    lines.foreach( function (address){
        addressarray = address.replace(/^ss*/, '').replace(/ss*$/, '').split(" ");
        obj[addressarray[0]] = !isnan(parseint(addressarray[1])) ? parseint(addressarray[1]) : addressarray[1];        
    });
    obj.building = "";
    obj.intercom = "";
    useraddresslist.push(obj);
    x.useraddresslist = useraddresslist; // convert field to string
    db.collection.save(x);
});
By Justin Mclean on June 11 2022

Answers related to “how to convert string to array in mongodb?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.