Home > front end >  Message schema that has multiple refs in mongodb using mongoose
Message schema that has multiple refs in mongodb using mongoose

Time:05-19

I am getting stuck here because I'm not sure how to set multiple ref's to an object to specify the objectid of it.

For example: (how can I specify the sender to be a Seller or it can be a User, also, what would be the best approach if there is for this situation)

const messageSchema = mongoose.Schema({
  message: {
    text: { type: String, required: true },
    type: String, //type can be or 'message', 'image',
  },
  members: {
    seller: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Seller",
      required: true,
    },
    client: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
  },
  sender: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
    required: true,
  },
  readOn: { type: Date },
  read: Boolean, //}, {
  timestamps: true,
});

I want to be able to say:

sender: {
    type: mongoose.Schema.Types.ObjectId,
    ref: ["User", "Seller"],
    required: true,
  },

would this be possible? Thanks a lot in advance.

CodePudding user response:

you can use refPath for dynamic referencing.

const messageSchema = mongoose.Schema({
  
 sender_type: String  // user or seller

 sender: {
    type: Schema.Types.ObjectId,
    refPath: 'sender_type'
  }
})

OR

you can use virtuals

const messageSchema = new mongoose.Schema({
...
 sender: {
    type: mongoose.Schema.Types.ObjectId,
    required: true
  },
...
},
{
    toJSON: { virtuals: true }
});

Then define virtual schemas like this:

order_schema.virtual('User', {
    ref: 'User', // Your User model name
    localField: 'sender', 
    foreignField: '_id', 
    justOne: true
});

order_schema.virtual('Seller', {
    ref: 'Seller',
    localField: 'sender',
    foreignField: '_id',
    justOne: true
});

Then in the database operations define two populates:

Model.find(search_filter)
      .populate('User')
      .populate('Seller')
  • Related