有效地从另一个猫鼬库中获取用户名

时间:2018-12-16 16:57:15

标签: node.js mongoose

在呈现显示所有产品以及拥有每个产品的用户的产品(用户名)的表时,如何轻松获取用户架构username

由于某种原因,我什么也没回来,而不是return user.username

将1000个产品呈现到表中并从数据库询问用户架构1000次时,效率很低。有什么更好的方法呢?我正在使用把手来渲染表,并使用猫鼬来查询数据。

用户模型:

const UserSchema = new Schema({
  username: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  }
});

产品型号:

const ProductSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  amount: {
    type: Number,
    required: true
  },
  ownerID: {
    //The ID of Users model
    type: String,
    required: true
  }
});

把手代码(表格行部分。即时通讯遍历我所有的产品):

<td>{{returnProductsOwner this.ownerID}}</td>
<td>{{this.name}}</td>
<td>{{this.amount}}</td>

ReturnProductsOwner:

returnProductsOwner: function(id) {
      User.findById(id).then((user) => {
        return user.username;
      }).catch();
    }

1 个答案:

答案 0 :(得分:0)

使用no-sql数据库时,为什么要分别保存用户详细信息? No-sql db无法很好地处理联接。因此,您可以做的是将用户详细信息与产品详细信息本身一起存储,以便您可以更快,更有效地查询它。