0

collection is a master collection

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;

collection is a children collection

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;

Requires to show all the values from master collection & a field related needs to be pushed in the main to show it's in 2nd collection please suggest the relevant method to implement that

1 Answers1

0

declare

Schema = mongoose.Schema;

set the type of the preferences to Schema.ObjectId instead of String. Now when u select the list of users or user byId or .. populate the preferences attribute Exemple : User.find().populate('preferences') ..... u can find example of mongoose populate

E.Aymen
  • 1
  • 2