简体   繁体   English

如何在使用 mongoose 架构时将动态数据存储在 mongodb 中?

[英]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.并且存储在 userSchema 中的数据将是动态的,我不知道,甚至字段也不知道,(我正在使用 auth0 获取注册时的用户数据)并且我想直接存储它。 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:因此,我基本上想存储 userSchema 中的任何内容,但如果我将其定义为空并保存一些数据,如下所示:

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.我拒绝所有数据,只生成_id。 How can I achieve this?我怎样才能做到这一点?

Try setting the strict option to false.尝试将 strict 选项设置为 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这是文档https://mongoosejs.com/docs/guide.html#strict中的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM