Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js MongoDB:嵌入或引用用户配置文件和地址_Node.js_Mongodb_Mongodb Query_Nosql - Fatal编程技术网

Node.js MongoDB:嵌入或引用用户配置文件和地址

Node.js MongoDB:嵌入或引用用户配置文件和地址,node.js,mongodb,mongodb-query,nosql,Node.js,Mongodb,Mongodb Query,Nosql,我只需要一点建议 我正在使用node.js和React以及mongoDb 我只是想看看我的模式架构(特别是地址)的配置文件,并建议我哪一个更好 方法1: const userSchema = new mongoose.Schema ({ userName: { type: String, required: true, minlength: 4, maxlength: 15, unique: true, }, email: { typ

我只需要一点建议

我正在使用node.js和React以及mongoDb

我只是想看看我的模式架构(特别是地址)的配置文件,并建议我哪一个更好

方法1:


const userSchema = new mongoose.Schema ({
  userName: {
    type: String,
    required: true,
    minlength: 4,
    maxlength: 15,
    unique: true,
  },
  email: {
    type: String,
    required: true,
    unique: true,
  },
  password: {
    type: String,
    required: true,
    minlength: 6,
    maxlength: 1024,
  },
  isVerified: {
    type: Boolean,
    default: false,
  },
  role: {
    type: String,
    enum: user_roles,
    default: 'user',
  },
  firstName: {
    type: String,
    max: 20,
    min: 2,
  },
  middleName: {
    type: String,
    max: 20,
    min: 2,
  },
  lastName: {
    type: String,
    max: 20,
    min: 2,
  },
  gender: {
    type: String,
    enum: genderVal,
  },
  dateOfBirth: {
    type: Date,
    trim: true,
  },
  phone : {
    type:String,
    default : ''
  }, 
  address: {
    address_name : {
      type: String,
    },
    address_line_1: {
      type: String,
    },
    address_line_2: {
      type: String,
    },
    city: {
      type: String,
    },
    state: {
      type: String,
    },
    landmark: {
      type: String,
    },
    country_code: {
      type: String,
    },
    zip_code: {
      type: String,
    },
    phone: {
      primary: {
        type: String,
        max: 15,
        default : ''
      },
      alternate: {
        type: String,
        max: 15,
        default : ''
      },
    },
  },
  profileImg: {
    type: String,
  },
});
我也尝试过这个方法,但插入地址,然后查询地址,因为它是嵌套模式,这是不必要的麻烦 . [康斯坦丁·斯莫莱宁(长稿)的答复] 在react中不能考虑嵌套状态

方法2

const userSchema = new mongoose.Schema ({
  userName: {
    type: String,
    required: true,
    minlength: 4,
    maxlength: 15,
    unique: true,
  },
  email: {
    type: String,
    required: true,
    unique: true,
  },
  password: {
    type: String,
    required: true,
    minlength: 6,
    maxlength: 1024,
  },
  isVerified: {
    type: Boolean,
    default: false,
  },
  role: {
    type: String,
    enum: user_roles,
    default: 'user',
  },
  firstName: {
    type: String,
    max: 20,
    min: 2,
  },
  middleName: {
    type: String,
    max: 20,
    min: 2,
  },
  lastName: {
    type: String,
    max: 20,
    min: 2,
  },
  gender: {
    type: String,
    enum: genderVal,
  },
  dateOfBirth: {
    type: Date,
    trim: true,
  },
  phone : {
    type:String,
    default : ''
  }, 
  profileImg: {
    type: String,
  },
});
const User = mongoose.model ('Users', userSchema);

Second Collection
const addrSchema = new mongoose.Schema ({
address: {
    user: { 
         type: mongoose.Schema.Types.ObjectId, 
         ref: 'Users' 
    },
    address_name : {
      type: String,
    },
    address_line_1: {
      type: String,
    },
    address_line_2: {
      type: String,
    },
    city: {
      type: String,
    },
    state: {
      type: String,
    },
    landmark: {
      type: String,
    },
    country_code: {
      type: String,
    },
    zip_code: {
      type: String,
    },
    phone: {
      primary: {
        type: String,
        max: 15,
        default : ''
      },
      alternate: {
        type: String,
        max: 15,
        default : ''
      },
    },
  },
});
const User = mongoose.model ('Address', addrSchema);
我可以将地址存储到另一个集合中