【问题标题】:Converting to Mongo/mongoose from Mysql with relations使用关系从 Mysql 转换为 Mongo/mongoose
【发布时间】:2020-01-15 03:30:19
【问题描述】:

在一个新项目中,我开始使用 MySQL,但我想将其转换为 mongodb/moongoose。

我的痛苦是我在 MySQL 中的不同关系。

F.x.我有用户表

uid | firstname | lastname | email

我是可容纳的

hid | address | zipcode | city | ownerid

其中 ownerid 是用户表中的用户

我有点不确定我应该如何在 mongoose 中设计我的模式,以便有一个有点像 MySQL 的选项。

我当前的用户架构是这样的

const userSchema = new mongoose.Schema({
    firstname: String,
    lastname: String,
    email: String,
    phone: Number,
    password: String,
    address: String,
    zipcode: Number,
    city: String,
    bankname: String,
    bankregno: Number,
    bankaccno: Number,
    userconfirmed: Boolean,
    confirmtoken: String,
    resettoken: String,
    resettokenexpires: Date,
    acceptmarketing: Boolean,
    lastloggedin: Date,
    disabled: Boolean
})

【问题讨论】:

    标签: javascript mysql node.js mongodb mongoose


    【解决方案1】:

    您可以在 userSchema 中为一组房屋添加一个字段,以将其分解并允许一对多关系。您可以使用subdocuments 来执行此操作。

    这可能看起来像这样。

    const houseSchema = new mongoose.Schema({
        address: String,
        zipcode: Number,
        city: String,
    })
    
    const userSchema = new mongoose.Schema({
        firstname: String,
        lastname: String,
        email: String,
        phone: Number,
        password: String,
        bankname: String,
        bankregno: Number,
        bankaccno: Number,
        userconfirmed: Boolean,
        confirmtoken: String,
        resettoken: String,
        resettokenexpires: Date,
        acceptmarketing: Boolean,
        lastloggedin: Date,
        disabled: Boolean,
        houses: [houseSchema],
    })
    

    【讨论】:

      【解决方案2】:

      你可以使用猫鼬populate,像这样

      const User = new mongoose.Schema({
          House: { type: mongoose.Types.Schema.ObjectId, ref: "House" }
      });
      
      const House = new mongoose.Schema({
          Number,
          Road: String
      });
      
      (async () => {
          const users = await User.find({}).populate("House");
      
          // users = [
          //     {
          //         _id: 373890hfjklfhjkfh,
          //         House: {
          //             _id: dhkdhdlhjkld,
          //             Number: 378930,
          //             Road: fhjdlhdjkld
          //         }
          //     }
          // ]
      })();
      
      

      【讨论】:

        【解决方案3】:

        如果您希望将这两种模式关联起来,这可能会对您有所帮助。如果您想了解更多信息,https://mongoosejs.com/docs/2.7.x/docs/populate.html

        const User = mongoose.model('User', new mongoose.Schema({
          firstname: {
          type: String,
         },
          lastname: {
            type: String,
         },
          email: {
            type: String
         }
        }));
        
        const Address = mongoose.model('Address', new mongoose.Schema({
          owner: {
            type: Schema.Types.ObjectId,
            ref: 'User'
           }
           //rest of your address schema
        }))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-23
          相关资源
          最近更新 更多