【问题标题】:NestJs mongoose nested populate not working as expectedNestJs mongoose 嵌套填充未按预期工作
【发布时间】:2021-07-22 03:45:48
【问题描述】:

我尝试了所有可能的语法来弄清楚如何在 mongoose 中填充数据,但没有一个有效,这是我的示例:

  1. 每个客户都有一组人员
  2. 每个人都有一个地址
  3. 每个客户也有一个地址 我想将客户连同人员及其地址一起填充。它只返回人员的数据,但不返回地址数据。

我试过了:

 this._customerModel
.findOne({ _id: '60898b500870d6a664fc7403' })
.populate({ path: 'persons', populate: { path: 'persons.address' } });

还有这个:

 this._customerModel
.findOne({ _id: '60898b500870d6a664fc7403' })
.populate({ path: 'persons' });

还有这个:

 this._customerModel
.findOne({ _id: '60898b500870d6a664fc7403' })
.populate({ path: 'persons', model : 'Person' })

还有这个:

 this._customerModel
.findOne({ _id: '60898b500870d6a664fc7403' })
.populate('persons');

他们都返回这个:

{
  address: 60898b500870d6a664fc7404,
  persons: [],
  _id: 60898b500870d6a664fc7403,
  siren: 'string',
  companyName: 'string',
  legalForm: 'string',
  startDate: 'string',
  __v: 0
}

如您所见,它甚至不返回人员内部的 ID。如果我不填充任何内容,它会返回:

{
  address: 60898b500870d6a664fc7404,
  persons: [ 60898b500870d6a664fc7405, 60898b500870d6a664fc7408 ],
  _id: 60898b500870d6a664fc7403,
  siren: 'string',
  companyName: 'string',
  legalForm: 'string',
  startDate: 'string',
  __v: 0
}

如您所见,数据就在那里。 地址实际上是一样的。它不会被填充,它总是返回的 ObjectId。 我正在注入这样的模型:

  MongooseModule.forFeature(
            [
                {
                    name: 'Customer',
                    schema: customerSchema,
                    collection: 'customers',
                },
            ],
            'nosql',
        )

我查看了互联网。 NestJs 中没有 3 级嵌套 populate() 的示例 如果那里有,或者您可能知道解决方案,如果您告诉我,我将不胜感激,谢谢。

@Schema()
export class Customer extends mongoose.Document {
@Prop()
id: string;
@Prop({ type: String })
companyName: string;
@Prop({ type: { type: mongoose.Schema.Types.ObjectId, ref: 'Address' } })
address: Address;
@Prop({ type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Person' }] })
persons: Person[];
}

@Schema()
export class Person extends mongoose.Document {
@Prop()
id: string;
@Prop({ type: String })
firstName: string;
@Prop({ type: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer' } })
customer: Customer;
@Prop({ type: { type: mongoose.Schema.Types.ObjectId, ref: 'Address' } })
address: Address;
}


@Schema()
export class Address extends mongoose.Document {
@Prop()
id: string;
@Prop({ type: String })
address1: string;
}

更新: 这个

this._customerModel
            .findOne({ _id: '60898b500870d6a664fc7403' })
            .populate('address')
            .populate('persons')

返回这个:

{
  address: 60898b500870d6a664fc7404,
  persons: [
    {
      customer: 60898b500870d6a664fc7403,
      _id: 60898b500870d6a664fc7405,
      firstName: 'string',
      __v: 0
    },
    {
      address: 60898b500870d6a664fc7406,
      customer: 60898b500870d6a664fc7403,
      _id: 60898b500870d6a664fc7408,
      firstName: 'string',
      __v: 0
    }
  ],
  _id: 60898b500870d6a664fc7403,
  companyName: 'string',
}

仍然没有返回地址(1 级),也没有返回我尝试过的人下的地址。

【问题讨论】:

  • 你试过把 .populate('addresses') 我知道名字是复数的
  • this._customerModel .findOne({ _id: '60898b500870d6a664fc7403' }) .populate('address') .populate('persons') 在这里,而不是 .populate('address'),试试.populate('地址')
  • 是的,很抱歉,我想我在完全加载之前发现了你的评论,哈哈,是的,我试过了,它返回的响应与上次更新相同,就像这样 :.findOne({ _id: ' 60898b500870d6a664fc7403' }) .populate('address') .populate('persons')

标签: javascript typescript mongoose nosql nestjs


【解决方案1】:

我在尝试制作一个 git 示例后发现它,这就是问题所在:

@Prop({ type: { type: mongoose.Schema.Types.ObjectId, ref: 'Address' } })

应该是这样的:

@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Address' })

当然在Array类型中也是。 对不起,互联网。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-15
    • 2017-08-20
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    相关资源
    最近更新 更多