简体   繁体   English

如何从猫鼬中的另一个模型引用字段

[英]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.我正在尝试根据用户名链接 Card 架构中用户模型中的一些字段。 So for example this is my Card schema例如,这是我的 Card 架构

 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.我需要的是在 Card 架构中始终拥有与具有该用户名的用户相同的 userSticker。 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.在创建卡片时添加它是行不通的,因为 userSticker 可能会发生变化,我希望在发生这种情况时卡片架构中的字段也发生变化,所以我想它应该是一个参考。

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查看关系数据库设计到 mongoDB/mongoose 设计

This section is for MongoDB本节适用于 MongoDB

There is two solution here since MongoDB is not a relational database like SQL.这里有两个解决方案,因为 MongoDB 不是像 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然后每当 userSticker 更改时,您都需要查询卡片集合并更新所有与用户名匹配的 userSticker

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然后当您查询证件卡集合时,您可以对 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... */})

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

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