首页 > 解决方案 > 填充没有 _id

问题描述

我有 2 个猫鼬模式,一个用于stock(有关股票的信息),一个用于trade. 其中交易代表交易stock(如时间、交易量等)。每个stock都有一个符号代码,我从中获取交易的数据馈送包括作为字符串的符号代码。我将如何填充这两个集合,因为我不能在'ref'这里使用常规的猫鼬。

这是我的两个模式:

const stockSchema = new Schema({
  symbolCode: { type: String, trim: true },
  symbol: { type: String, trim: true },
  type: { type: String, index: true, trim: true },
  country: { type: String, lowercase: true }
})

 const tradeSchema = new Schema({
  symbolCode: { type: String, index: true },
  symbol: { type: String, index: true },
  price: Number,
  volume: Number,
  time: Date,
  currency: { type: String, default: 'USD', uppercase: true, index: true }
})

我想删除trade架构中的前两个字段,以便我可以在这里对股票进行某种引用。我怎样才能做到这一点?

标签: mongodbmongoosemongoose-populate

解决方案


像这样使用填充: MyModel.populate([{ path: 'author', select: 'username name -_id' }]);

-fieldName或在您的情况下将从-_id投影中扣除。


推荐阅读