我尝试了所有可能的语法来弄清楚如何在猫鼬中填充数据,但没有一个有效,这是我的例子:
我试过这个:
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级)或人员下的地址。
我在尝试制作一个 git 示例后发现了它,这就是问题所在:
@Prop({ type: { type: mongoose.Schema.Types.ObjectId, ref: 'Address' } })
应该是这样的:
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Address' })
当然在数组类型中也是如此。 对不起互联网。
我有类似的问题,但对我来说问题是使用了不正确的类型:
错误的做法:
请注意,我们有“类型”导入
import mongoose, { HydratedDocument, Types } from 'mongoose';
@Prop({
type: [{ type: Types.ObjectId, ref: 'Attachment' }],
default: [],
})
attachments: Attachment[];
正确做法:
import mongoose, { HydratedDocument, Types } from 'mongoose';
@Prop({
type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Attachment' }],
default: [],
})
attachments: Attachment[];