如何按填充字段查找文档?

时间:2019-09-23 06:28:09

标签: node.js mongodb mongoose nestjs

如何按他的孩子所在的领域查找文档?

我如何找到联系人姓名为John的承包商?
还有
如何找到承包商名称为Company的联系人?

ContactModel.find({company.name: 'Company'}) 

不起作用,因为在DB公司中只有一个ID

我有2种模式:

export const ContractorSchema = new mongoose.Schema({
  adresses: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Address' }],
  contacts: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Contact' }],
  company: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Company' }],
  name: { type: 'String', required: true },
  nameLong: { type: 'String', required: true },
  nip: { type: 'String', required: true },
});

export const ContactSchema = new mongoose.Schema({
  firstName: { type: 'String', required: true },
  lastName: { type: 'String', required: true },
  telephone: { type: 'String', required: true },
  email: { type: 'String', required: true },
  position: { type: 'String', required: true },
  contractor: { type: mongoose.Schema.Types.ObjectId, ref: 'Contractor' },
});

0 个答案:

没有答案