简体   繁体   中英

How can I reference a field from another model in mongoose

I am trying to link some fields from the User model in the Card schema based on the username. So for example this is my Card schema

 const CardSchema = new mongoose.Schema({
      text: {
        type: String,
      },
      username: {
       type: String,
       ref: 'User',
       required: true
      },
      userSticker: {
       This is what I need to get from the user model based on the username or user id
      }

And this is what the user model looks like:

const UserSchema = new mongoose.Schema({
  username: {
    type: String,
  },
  userSticker: {
   type: String,
  }

What I would need is to have always in the Card schema the same userSticker as the user with that username has. Adding it when the card is created will not work because the userSticker may change and I would like the field to change in the Card schema too when that happens, so I guess it should be something like a reference.

UPDATE : Okay so I've read that mongoose does this for you. It can model your tables relationally and populate relational data based on the ref you defined in the schema.

Check this out Relational database design to mongoDB/mongoose design

This section is for MongoDB

There is two solution here since MongoDB is not a relational database like SQL.

First solution is to duplicate the data and update all fields whenever the field value change

const CardSchema = new mongoose.Schema({
  text: {
    type: String,
  },
  username: {
    type: String,
  },
  userSticker: {
    type: String,
  }
})

const UserSchema = new mongoose.Schema({
  username: {
    type: String,
  },
  userSticker: {
    type: String,
  }
})

then whenever userSticker change you need to query card collection and update all userSticker matching the username

Second solution is two create manually a reference between collections

const CardSchema = new mongoose.Schema({
  text: {
    type: String,
  },
  user_id: {
    type: String
  }
})

const UserSchema = new mongoose.Schema({
  _id: {
    type: String,
  },
  username: {
    type: String,
  },
  userSticker: {
    type: String,
  }
})

then when you query the document card collection you can do a second query for the document referenced by user_id

  • First one is slower on write but faster on read
  • Second one is faster on write but slower on read (paginate your query)

From official mongoose docs Query Population

 const cardSchema = new mongoose.Schema({
      text: {
        type: String,
      },
      username: {
       type: String,
       ref: 'User',
       required: true
      },
      userSticker: {
       type: Schema.Types.ObjectId, 
       ref: 'User'
      }
};

const userSchema = new mongoose.Schema({
  username: {
    type: String,
  },
  userSticker: {
   type: String,
  }
}

const User = mongoose.model('User', userSchema);
const Card = mongoose.model('Card', cardSchema);

Card.findbyId(id).populate('userSticker', 'userSticker').exec(function(err, card) {/* Do stuff... */})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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