简体   繁体   中英

How to get reference of one schema, into another?

I have a schema, named airline schema, like this:

id:{type:Number},

name:{type:Number},

Country:{type:Number},

logo:{type:Number},

slogan:{type:Number},

head_quaters:{type:Number},

website:{type:Number},

I have to pass this schema, into another schema of passengers, like this

name:{type:String},

trips:{type:Number},

airline:??????

Now, if I fetch passengers details by its unique id given by Mongo DB, then all the details of passenger and its airline details must be seen like this

name: "Ashish"

trips:"230"

airline:[{

id:5

name:air india
.......}]

How can I do this?

I guess each passenger used many airlines to make those trips.

So you can have the airline _ids as an array in each passenger, and do a lookup using this array.

Passenger

{name : ...
 trips : ...
 airline : [_id _id ...]   //all the airline_id he used 
}

And to combine this information you can do.
(we can use array field also as $lookup field, it will join if equal with any member)

passengers.aggregate(
[{"$lookup":
  {"from":"airline",
   "localField":"airline",
   "foreignField":"_id",
   "as":"airlines"}}])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM