NestJs 猫鼬嵌套填充无法按预期工作
NestJs mongoose nested populate not working as expected
我尝试了所有可能的语法来弄清楚如何在 mongoose 中填充数据,但是 none 工作正常,这是我的示例:
- 每个客户都有一组人员
- 每个人都有一个地址
- 每个客户也有一个地址
我想填充客户以及人员及其地址。它只有 return 个人数据,没有地址数据。
我试过了:
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');
他们都是return这个:
{
address: 60898b500870d6a664fc7404,
persons: [],
_id: 60898b500870d6a664fc7403,
siren: 'string',
companyName: 'string',
legalForm: 'string',
startDate: 'string',
__v: 0
}
如您所见,它甚至没有 return 人员内部的 ID。如果我不填充任何内容,它 return 是这样的:
{
address: 60898b500870d6a664fc7404,
persons: [ 60898b500870d6a664fc7405, 60898b500870d6a664fc7408 ],
_id: 60898b500870d6a664fc7403,
siren: 'string',
companyName: 'string',
legalForm: 'string',
startDate: 'string',
__v: 0
}
如您所见,数据就在那里。
地址实际上是一样的。它不会被填充它总是 ObjectId 得到 returned。
我正在像这样注入模型:
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')
return这是:
{
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',
}
仍然没有 return 地址(1 级),也没有经过我的任何尝试的人的地址。
我在尝试制作一个 git 示例后发现它,这就是问题所在:
@Prop({ type: { type: mongoose.Schema.Types.ObjectId, ref: 'Address' } })
应该是这样的:
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Address' })
当然在数组类型中也是如此。
对不起网络。
我尝试了所有可能的语法来弄清楚如何在 mongoose 中填充数据,但是 none 工作正常,这是我的示例:
- 每个客户都有一组人员
- 每个人都有一个地址
- 每个客户也有一个地址 我想填充客户以及人员及其地址。它只有 return 个人数据,没有地址数据。
我试过了:
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');
他们都是return这个:
{
address: 60898b500870d6a664fc7404,
persons: [],
_id: 60898b500870d6a664fc7403,
siren: 'string',
companyName: 'string',
legalForm: 'string',
startDate: 'string',
__v: 0
}
如您所见,它甚至没有 return 人员内部的 ID。如果我不填充任何内容,它 return 是这样的:
{
address: 60898b500870d6a664fc7404,
persons: [ 60898b500870d6a664fc7405, 60898b500870d6a664fc7408 ],
_id: 60898b500870d6a664fc7403,
siren: 'string',
companyName: 'string',
legalForm: 'string',
startDate: 'string',
__v: 0
}
如您所见,数据就在那里。 地址实际上是一样的。它不会被填充它总是 ObjectId 得到 returned。 我正在像这样注入模型:
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')
return这是:
{
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',
}
仍然没有 return 地址(1 级),也没有经过我的任何尝试的人的地址。
我在尝试制作一个 git 示例后发现它,这就是问题所在:
@Prop({ type: { type: mongoose.Schema.Types.ObjectId, ref: 'Address' } })
应该是这样的:
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Address' })
当然在数组类型中也是如此。 对不起网络。