Javascript NestJs mongoose嵌套填充未按预期工作
我尝试了所有可能的语法来找出如何在mongoose中填充数据,但没有一个有效,以下是我的示例:Javascript NestJs mongoose嵌套填充未按预期工作,javascript,typescript,mongoose,nosql,nestjs,Javascript,Typescript,Mongoose,Nosql,Nestjs,我尝试了所有可能的语法来找出如何在mongoose中填充数据,但没有一个有效,以下是我的示例: 每个客户都有一系列人员 每个人都有一个地址 此外,每个客户都有一个地址 我希望填充客户以及人员及其地址。它只返回人员的数据,但不返回地址数据 我试过这个: this._customerModel .findOne({ _id: '60898b500870d6a664fc7403' }) .populate({ path: 'persons', populate: { path: 'persons.a
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');
这是:
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');
这是:
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
}
{
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',
}
正如你所看到的,数据就在那里。
地址其实是一样的。它不会被填充,返回的总是ObjectId。
我注射的模型如下:
MongooseModule.forFeature(
[
{
name: 'Customer',
schema: customerSchema,
collection: 'customers',
},
],
'nosql',
)
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Address' })
我在网上查过。在NestJs中没有3级嵌套填充()的示例
如果有,或者你可能知道解决方案,如果你让我知道,我将不胜感激,谢谢
@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;
}
更新:
这个
返回以下内容:
{
address: 60898b500870d6a664fc7404,
persons: [ 60898b500870d6a664fc7405, 60898b500870d6a664fc7408 ],
_id: 60898b500870d6a664fc7403,
siren: 'string',
companyName: 'string',
legalForm: 'string',
startDate: 'string',
__v: 0
}
{
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),也不会返回persons下的地址(我的任何尝试)。我在尝试制作git示例后找到了它,这就是问题所在:
@Prop({ type: { type: mongoose.Schema.Types.ObjectId, ref: 'Address' } })
应该是这样的:
MongooseModule.forFeature(
[
{
name: 'Customer',
schema: customerSchema,
collection: 'customers',
},
],
'nosql',
)
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Address' })
当然在数组类型中也是如此。
很抱歉,internet。您是否尝试过将.populate('address')放在此处?我知道名称是复数的。请使用.populate({u id:'60898b500870d6a664fc7403')。填充('address')。在此处填充('persons'),而不是.populate('address'),尝试使用.populate('address'))是的,很抱歉,我想我在完全加载lol之前看到了你的评论,是的,我尝试了,它返回的响应与上次更新相同,如下所示:.findOne({{u id:'60898b500870d6a664fc7403')).populate('address').populate('persons'))