How to store dynamic data in mongodb while using mongoose schema?

Lets say I have a user schema as such:

const userschema = new mongoose.Schema({

And the data stored in userSchema will be dynamic, it is not pre-known to me, not even the fields,(I am using the auth0 to get user data on signup) and I want to directly store it. I don't need any data sanitization for that. But the data might not always have same key value pairs. Hence I basically want to store whatever comes in to the userSchema, but if I define it as empty and save some data like this:

const data = {
      name: 'vini',
      array: [{name:'vini'}, {name:'jaya'}, {name:'123'}]
const user = await User.create(data);

I rejects all the data and only generates the _id. How can I achieve this?

Read more here:

Content Attribution

This content was originally published by Vinita at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: