猫鼬接after而至

时间:2019-05-30 14:19:23

标签: arrays join mongoose

我有3个mongo集合。

  1. 资源
  2. 访问
  3. 用户

用户与访问有关系,而访问与资源有关系。我想在填充用户时完成所有连接,然后看到用户可以访问它们的完整资源。

我尝试了此代码,但未预期到响应

     Users.findById(req.params.userId)
     .populate({path:'accessID',model:'accesses'})
     .populate({path:'accessID.accessLists',model:'resources'})

还有这个

 Users.findById(req.params.userId)
     .populate({path:'accessID',model:'accesses'})
     .then
(
        (user)=>
        {
         Users.populate(user,{path})
        }
)



这是我的叛逃

资源:

  var ResourceSchema =new Schema 
       ({  
        resource:String
        method:
        {   type:String,
            enum:["GET","PUT","POST","DELETE"]
        } 
       })

访问权限:

     var AccessSchema =new Schema 
     ({
      name:String
      access_List:
     [
       { type: Schema.Types.ObjectId,  ref:'resources'}
     ]

    })

用户:

     var  UserSchema = new Schema
      ({
       name:String,
       email:String,
       accessID:
       { type: Schema.Types.ObjectId, required: true, ref: 'accesses' }
      )}  

我的输出是这样的

{"accessID": 
 {
   "_id": "5cef82d1d2f7e14d3013ac8c",
           "accessLists": 
                [
               "5cee8e093defe92b88c1626b",
               "5cee8e093defe92b88c1626c",
               "5cee8e093defe92b88c1626d",
                 ],
           "name": "Admin",       
   },
"name": "ali"
"email": "ali17@yahoo.com"      
      }

但是我想要这样的输出:

 {
  "name": "ali",
   "email": "ali17@yahoo.com"
   "accessID": 
    {
       "_id": "5cef82d1d2f7e14d3013ac8c",
         "accessLists": 
       [
           {
                "_id": "5cee8e093defe92b88c1626b",
                "resource": "groups",
                "method": "POST",
            },
            {
                "_id": "5cee8e093defe92b88c1626c",
                "resource": "groups",
                "method": "DELETE",
            },
            {
                "_id": "5cee8e093defe92b88c1626d",
                "resource": "groups",
                "method": "GET",
            }
       ]
            "name": "Admin",       
          }

         }

1 个答案:

答案 0 :(得分:0)

    User.findById(req.params.userId)
        .populate({path:'accessID',model:'accesses'})
        .populate({ path: 'accessID', populate: { path: 'access_List',model:'resources' }})
        .exec(function (err, response) {
     })

我已经尝试了并且可以正常工作。 更多帮助:Example

另外,我已经尝试过聚合:

db.users.aggregate([
   {$match : { _id : ObjectId("5cf00388eae5400e9439b2df")}},
   {
        $lookup: {
            from:"access",
            localField:"accessID",
            foreignField: "_id",
            as:"accessID"
        }
    },
    { "$unwind": '$accessID'},
    { "$unwind": '$accessID.access_List'},
    {
        $lookup: {
            from:"resources",
            localField:"accessID.access_List",
            foreignField: "_id",
            as:"access_List"
        }
    },
    {
        $group : {
            _id : {
                "_id" : '$_id', "name" : '$name', "email" : '$email',
                 "accessID" : {"_id" : '$accessID._id', "name": '$accessID.name' }
            },
            access_List : { $push : { "$arrayElemAt": [ "$access_List", 0 ] } }
        }
    },
    { $project : {
          _id : '$_id._id',
          name : '$_id.name',
          email : '$_id.email',
          accessID : {
              _id : '$_id.accessID._id',
              name:  '$_id.accessID.name',
              access_List : '$access_List'
          }

       }
    }
    ]).pretty()