是否可以相对于猫鼬中非_id的其他字段填充数据

时间:2020-08-10 14:06:13

标签: node.js mongodb express mongoose

填充data booking_unique_id的所有时间一直在给我null

以下是架构:

const chat = require('../models/chat.model')
const booking_details = new Schema({
    booking_unique_id:{type:Object,ref:chat,field:'chat_screen_id'}
    });
const chat_details = new Schema({
    ...
    receiver_public_name:{type:String}
    chat_screen_id:{type:Object}
    });
Booking.find({booking_status:'e'}).populate('booking_unique_id'))

1 个答案:

答案 0 :(得分:0)

ref populate当前不支持它,猫鼬Issue-3225Issue-1888中存在问题,

作为替代方案,他们发布了populate-virtuals

  • 聊天模式
const chat_details = new Schema({
  ...
  receiver_public_name: { type: String }
  chat_screen_id: { type: Object }
});
  • 预订模式
const chat = require('../models/chat.model');
const booking_details = new Schema({
  booking_unique_id: { type: Object }
});
  • 虚拟预订
booking_details.virtual('bookings', {
  ref: chat, // The model to use
  localField: 'booking_unique_id', // Find booking where `localField`
  foreignField: 'chat_screen_id', // is equal to `foreignField`
  // Query options, see /mongoose-query-options
  // options: { sort: { name: -1 }, limit: 5 } 
});
  • 预订模型
const Booking = mongoose.model('Booking', booking_details);
  • 通过填充预订查找查询
Booking.find({ booking_status: 'e' }).populate('bookings').exec(function(error, result) {
  console.log(result);
});
相关问题