【问题标题】:Join on collection in mongoodb using mongoose使用 mongoose 在 mongodb 中加入集合
【发布时间】:2018-08-21 15:03:42
【问题描述】:

收藏是主收藏

var FeedsSchema = new mongoose.Schema({
  categoryName: {
    type: String,
    unique: true,
    required: true,
    ref: 'feeds',
    trim: true
  },
  categoryLogo: {
    type: String,
    unique: false,
    required: true,
    trim: true
  },
  status:{
      type: String,
      required: true,
  },
  date:{
      type: String,
      required:true
  }
});
var categories = mongoose.model('categories', FeedsSchema);
module.exports = categories;

集合是子集合

var UserSchema = new mongoose.Schema({
  user_id: {
    type: String,
    ref: 'user',
    required: true,
    trim: true
  },
  preferences :{
    type: String,
    required: true,
    ref: 'categories',
    trim: true
  },
  status :{
    type: String,
    required: true,
    trim: true
  }
});
var Preferences = mongoose.model('preferences', UserSchema);
module.exports = Preferences;

需要显示主集合中的所有值,并且需要在主集合中推送相关字段以显示它在第二个集合中 请提出相关的实现方法

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    声明

    Schema = mongoose.Schema;
    

    将首选项的类型设置为 Schema.ObjectId 而不是 String。 现在,当您选择用户列表或用户 byId 或 .. 填充首选项属性 示例:User.find().populate('preferences') ..... 你可以找到mongoose populate的例子

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2018-03-24
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 2019-08-07
      • 2021-06-20
      • 2017-03-28
      • 1970-01-01
      相关资源
      最近更新 更多