mongo集合间的关联

//多集合关联查询,product集合关联orders集合,使用$lookup做集合之间的关联

//集合之间设计不太合理,说明问题即可

/* product集合 */
{

"_id" : 1.0,

"name" : "草莓",

"type" : "水果"

}


{

"_id" : 2.0,

"name" : "苹果",

"type" : "水果"

}

-----------------------------------------------------------------------------------------------------------------------------

/* orders集合 */
{

"_id" : "1",

"pid" : 1.0,

"ordername" : "订单1"

}

{

"_id" : "2",

"pid" : 2.0,

"ordername" : "订单2"

}


{
"_id" : "3",

"pid" : 2.0,

"ordername" : "订单3"

}


{

"_id" : "4",

"pid" : 1.0,

"ordername" : "订单4"

}

-----------------------------------------------------------------------------------------------------------------------------

db.product.aggregate([{

      $lookup:{

          from: "oders",

          localField: "_id", //指product的id字段

          foreignField: "pid", //指product的id字段在orders集合中的外键字段

          as: "results" //结果集名称

      }

}])

转载于:https://www.cnblogs.com/soren-wanglu/p/11211433.html

你可能感兴趣的:(mongo集合间的关联)