[英]How to add reference of one schema to another in Mongoose and inserting data by json?
[英]How to get reference of one schema, into another?
我有一個名為航空公司架構的架構,如下所示:
id:{type:Number},
name:{type:Number},
Country:{type:Number},
logo:{type:Number},
slogan:{type:Number},
head_quaters:{type:Number},
website:{type:Number},
我必須將這個模式傳遞到另一個乘客模式中,就像這樣
name:{type:String},
trips:{type:Number},
airline:??????
現在,如果我通過 Mongo DB 提供的唯一 ID 獲取乘客詳細信息,則必須像這樣查看乘客及其航空公司詳細信息的所有詳細信息
name: "Ashish"
trips:"230"
airline:[{
id:5
name:air india
.......}]
我怎樣才能做到這一點?
我猜每個乘客都使用了許多航空公司來進行這些旅行。
因此,您可以將airline _ids
作為每個乘客的數組,並使用此數組進行查找。
乘客
{name : ...
trips : ...
airline : [_id _id ...] //all the airline_id he used
}
並結合這些信息你可以做。
(我們也可以使用數組字段作為$lookup
字段,如果與任何成員相等,它將加入)
passengers.aggregate(
[{"$lookup":
{"from":"airline",
"localField":"airline",
"foreignField":"_id",
"as":"airlines"}}])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.