2表查询
const user_Schema = require("../model/user_Schema");//(第1个表)
const role_Schema = require("../model/role_Schema");//(第2个表)
const user_role_Schema = require("../model/t_user_role_Schema");
router.get('/',async(req,res)=>{
let result=await user_Schema.aggregate([
{
$lookup:
{
from: "role", //关联第二张表(角色)
localField: "pid",//user表的主键id (第1个表)
foreignField: "id_name_", //role角色 表 主键ID的字段 (第2个表role)
as: "items"
}
}
])
console.log(result);
res.send(result)
})
3表查询
router.get('/',async(req,res)=>{
let result=await user_Schema.aggregate([
{
lookup:
{
from: "t_user_role", //关联第二张表(角色)
localField: "pidxxx",//user表的主键id (第1个表)
foreignField: "id_namexxxx_", //role角色 表 主键ID的字段 (第2个表role)
as: "items"
}
}
])
console.log(result);
res.send(result)
})
module.exports=router;
4表查询之条件
router.get('/',async(req,res)=>{
let result=await user_Schema.aggregate([
{
match:{id_name:1}
}
])
console.log(result);
res.send(result)
})
module.exports=router;
[{"id":"60b0bc4c74d0832de80b8db2","id_name":1,"username":"陈陈","pid":1,"uid":1,"items":[{"id":"60b0a41e898fd52f8c99dc75","role_name":"管理员","id_name_":1}]}]