How I achieve a one-to-one relationship using Typeform MongoDB.
I m trying to join two documents using aggregation but not achieve success.
Please provide one solution.
Entity details 1.counrty :- _id,name 2.state :- _id,name, countryid
export class Country {
@ObjectIdColumn()
@Type(() => String)
_id: ObjectID;
@Column()
@IsString()
@IsNotEmpty()
@Index({unique: true})
name: string;
}
@Entity()
export class State {
@ObjectIdColumn()
@Type(() => String)
_id: ObjectID;
@Column()
@IsNotEmpty()
name: string;
@ObjectIdColumn({nullable: false})
@IsNotEmpty()
countryId: ObjectID;
}
find code(select all state)
let stateRepository: StateRepository = getCustomRepository(StateRepository);
try {
const states = await stateRepository.aggregate([
{
$lookup:
{
from: 'country',
localField: 'countryId',
foreignField: '_id',
as: 'country'
}
}
]);
res.status(200).send({
data: states
});
}
catch (exception) {
res.status(500).send({
error: exception,
});
}
}
Output:- receive 500 with no error
{
"error": {}
}