How to map an input field as different field in mongodb

Viewed 173

Suppose I am inserting data in MongoDB as

db.collection.insertMany([{username:'Jack',msg:'hello'},{username:'Jenny',msg:'hello world'},{username:'Rose',msg:'Hi howz You'}])

now I want this data to be inserted as this:

{account_username:'Jack',msg:'hello'}
{account_username:'Jenny',msg:'hello world'}
{account_username:'Rose',msg:'Hi howz You'}

and NOT THIS:

{username:'Jack',msg:'hello'}
{username:'Jenny',msg:'hello world'}
{username:'Rose',msg:'Hi howz You'}

I am using mongoose in my NodeJS code, can I do something in Schema so whenever input is username it will automatically refer to account_username of my defined mongoose Schema.

1 Answers

If using mongoose with mongoDB one can easily do this using mongoose virtual functions functionality, Mongoose Virtuals

Mongoose Viruals have Setters and Getters, Which have functionality to define columns with different name

Related