Skip to content

Mongoose does not allow . (dot) in keys, so this function can be used to make object keys of any depth, mongoose friendly keys.

Notifications You must be signed in to change notification settings

hasanzia1993/mongooseFriendlyKeys

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

mongooseFriendlyKeys

Mongoose does not allow . (dot) in keys, so this async function can be used to make object keys of any depth, mongoose friendly keys.

Usage

const response = await mongooseFriendly(object,' ');
console.log(response);

Function

mongooseFriendly = async (obj,replacementSymbol='_')=>{
    if(replacementSymbol==='.')
        throw "Cannot use . (dot) as replacement symbol use _ or __ etc";
    if(typeof obj === "object"){
        //get all object keys
        const keys = Object.keys(obj);
        var newObj = {};
        for(var i=0;i<keys.length;i++){                                                                                
            let key = keys[i];         
            //since mongoose does not allow . (dot) in keys, replace . with another symbol  
            let newKey = key.toString().replace('.',replacementSymbol);
            //send value of current object as the paramenter with the same replacement symbol
            newObj[newKey] = await mongooseFriendly(obj[keys[i]],replacementSymbol);
        }
        //return the new object with mongoose friendly keys
        return newObj;
    }
    //in case the parameter is not an object return the value
    return obj;
}

About

Mongoose does not allow . (dot) in keys, so this function can be used to make object keys of any depth, mongoose friendly keys.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published