Reputation: 1864
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'}],
age: 20,
gender: 'female',
others: {key:value}
};
const user = await User.create(data);
I rejects all the data and only generates the _id. How can I achieve this?
Upvotes: 1
Views: 933
Reputation: 91
Try setting the strict option to false. In your case
const userschema = new mongoose.Schema({
}, { strict: false});
Here is the link in the docs https://mongoosejs.com/docs/guide.html#strict
Upvotes: 2