匹配另一个子文档中的子文档相同的文档

时间:2015-06-04 18:54:38

标签: mongodb match project subdocument

我的收藏中有以下文件:

{
"_id": ObjectId('555a33d69487b45401ec1149'),
"nombre": "demo",
"roles": [
    {
        "id": ObjectId('556ca1999487009c1fac125f'),
        "nombre": "rol1"
    },
    {
        "id": ObjectId('556ca1a09487009c1fac1260'),
        "nombre": "rol2"
    },
    {
        "id": ObjectId('556ca1a69487009c1fac1261'),
        "nombre": "rol3"
    }
],
"usuarios": [
    {
        "id": ObjectId('556ca1de9487009c1fac1262'),
        "activo": true,
        "roles": [
            {
                "id": ObjectId('556ca1999487009c1fac125f')
            }
        ]
    },
    {
        "id": ObjectId('556ca2659487009c1fac1263'),
        "activo": true,
        "roles": [
            {
                "id": ObjectId('556ca1a09487009c1fac1260')
            }
        ]
    },
    {
        "id": ObjectId('556ca27a9487009c1fac1264'),
        "activo": true,
        "roles": [
            {
                "id": ObjectId('556ca1a69487009c1fac1261')
            }
        ]
    }
]
}


{
    "_id": ObjectId('556f457594877b4502b2e85d'),
    "nombre": "app3",
    "roles": [
        {
            "id": ObjectId('556f45fa94877b4502b2e864'),
            "nombre": "rol1_app3"
        },
        {
            "id": ObjectId('556f460194877b4502b2e865'),
            "nombre": "rol3_app3"
        },
        {
            "id": ObjectId('556f460694877b4502b2e866'),
            "nombre": "rol5_app3"
        },
        {
            "id": ObjectId('556f460b94877b4502b2e867'),
            "nombre": "rol7_app3"
        }
    ],
    "usuarios": [
        {
            "id": ObjectId('556ca1de9487009c1fac1262'),
            "activo": true,
            "roles": [
                {
                    "id": ObjectId('556f460b94877b4502b2e867')
                },
                {
                    "id": ObjectId('556f460194877b4502b2e865')
                }
            ]
        }
    ]
}

我想要的是指定' usuarios.id'并获得所有" roles.id" ," roles.nombre"由每个文件所在的' usuarios.id'存在。

我试过这个:

- 首先使用' usuarios.id'声明一个var。我正在寻找

var uid = new ObjectId("556ca1de9487009c1fac1262")

然后我执行这个:

db.aplicacion.aggregate([
{ $match: { "usuarios.id" : uid, "usuarios.activo" : true } },
{ $unwind: "$usuarios"},
{ $unwind: "$usuarios.roles"},
{ $match: { "usuarios.id" : uid } },
{ $unwind: "$roles"},
{ $project: { "nombre":1, "usuarios.roles.id":1 , "usuarios.id":1,      "roles.id":1, "roles.nombre":1 }}
]).pretty();

但结果如下:

    {
        "_id" : ObjectId("555a33d69487b45401ec1149"),
        "nombre" : "demo",
        "roles" : {
                "id" : ObjectId("556ca1999487009c1fac125f"),
                "nombre" : "rol1"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556ca1999487009c1fac125f")
                }
        }
}
{
        "_id" : ObjectId("555a33d69487b45401ec1149"),
        "nombre" : "demo",
        "roles" : {
                "id" : ObjectId("556ca1a09487009c1fac1260"),
                "nombre" : "rol2"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556ca1999487009c1fac125f")
                }
        }
}
{
        "_id" : ObjectId("555a33d69487b45401ec1149"),
        "nombre" : "demo",
        "roles" : {
                "id" : ObjectId("556ca1a69487009c1fac1261"),
                "nombre" : "rol3"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556ca1999487009c1fac125f")
                }
        }
}
{
        "_id" : ObjectId("556f457594877b4502b2e85d"),
        "nombre" : "app3",
        "roles" : {
                "id" : ObjectId("556f45fa94877b4502b2e864"),
                "nombre" : "rol1_app3"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556f460b94877b4502b2e867")
                }
        }
}
{
        "_id" : ObjectId("556f457594877b4502b2e85d"),
        "nombre" : "app3",
        "roles" : {
                "id" : ObjectId("556f460194877b4502b2e865"),
                "nombre" : "rol3_app3"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556f460b94877b4502b2e867")
                }
        }
}
{
        "_id" : ObjectId("556f457594877b4502b2e85d"),
        "nombre" : "app3",
        "roles" : {
                "id" : ObjectId("556f460694877b4502b2e866"),
                "nombre" : "rol5_app3"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556f460b94877b4502b2e867")
                }
        }
}
{
        "_id" : ObjectId("556f457594877b4502b2e85d"),
        "nombre" : "app3",
        "roles" : {
                "id" : ObjectId("556f460b94877b4502b2e867"),
                "nombre" : "rol7_app3"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556f460b94877b4502b2e867")
                }
        }
}
{
        "_id" : ObjectId("556f457594877b4502b2e85d"),
        "nombre" : "app3",
        "roles" : {
                "id" : ObjectId("556f45fa94877b4502b2e864"),
                "nombre" : "rol1_app3"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556f460194877b4502b2e865")
                }
        }
}
{
        "_id" : ObjectId("556f457594877b4502b2e85d"),
        "nombre" : "app3",
        "roles" : {
                "id" : ObjectId("556f460194877b4502b2e865"),
                "nombre" : "rol3_app3"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556f460194877b4502b2e865")
                }
        }
}
{
        "_id" : ObjectId("556f457594877b4502b2e85d"),
        "nombre" : "app3",
        "roles" : {
                "id" : ObjectId("556f460694877b4502b2e866"),
                "nombre" : "rol5_app3"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556f460194877b4502b2e865")
                }
        }
}
{
        "_id" : ObjectId("556f457594877b4502b2e85d"),
        "nombre" : "app3",
        "roles" : {
                "id" : ObjectId("556f460b94877b4502b2e867"),
                "nombre" : "rol7_app3"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556f460194877b4502b2e865")
                }
        }
}

这是每个rol.id的产品,我期望得到的是这样的东西(只有那些在哪里:roles.id = usuarios.roles.id:

    {
        "_id" : ObjectId("555a33d69487b45401ec1149"),
        "nombre" : "demo",
        "roles" : {
                "id" : ObjectId("556ca1999487009c1fac125f"),
                "nombre" : "rol1"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556ca1999487009c1fac125f")
                }
        }
},
{
        "_id" : ObjectId("556f457594877b4502b2e85d"),
        "nombre" : "app3",
        "roles" : {
                "id" : ObjectId("556f460b94877b4502b2e867"),
                "nombre" : "rol7_app3"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556f460b94877b4502b2e867")
                }
        }
},
{
        "_id" : ObjectId("556f457594877b4502b2e85d"),
        "nombre" : "app3",
        "roles" : {
                "id" : ObjectId("556f460194877b4502b2e865"),
                "nombre" : "rol3_app3"
        },
        "usuarios" : {
                "id" : ObjectId("556ca1de9487009c1fac1262"),
                "roles" : {
                        "id" : ObjectId("556f460194877b4502b2e865")
                }
        }
}

任何想法??

这是我目前的剧本:

var uid = new ObjectId("556ca1de9487009c1fac1262")

db.aplicacion.aggregate([
    { $match: { "usuarios.id" : uid, "usuarios.activo" : true } },
    { $unwind: "$usuarios"},
    { $unwind: "$usuarios.roles"},
    { $match: { "usuarios.id" : uid } },
    { $unwind: "$roles"},
    { $project: { "nombre":1, "usuarios.roles.id":1 , "usuarios.id":1,      "roles.id":1, "roles.nombre":1 }}
    ]).pretty();

更新

使用@yogesh解决方案后,我对脚本做了一点改进:

    db.coll.aggregate({
    "$unwind": "$usuarios"
}, {
    "$match": {
        "usuarios.id": ObjectId("556ca1de9487009c1fac1262"),
        "usuarios.activo":true,
    }
}, {
    "$unwind": "$usuarios.roles"
}, {
    "$unwind": "$roles"
}, {
    "$project": {
        "nombre": 1,
        "roles.id": 1,
        "roles.nombre": 1,        
        "roles.activo": 1,
        "matched": {
            "$eq": ["$roles.id", "$usuarios.roles.id"], //macthed roles.id == usuarios.roles.id            
        }
    }
}, {
    "$match": {
        "matched": true,
        "roles.activo" : true
    }
},
{
  $group : {
        _id : "$_id",
        nombre: { $first: "$nombre" },
        roles : {
                $push : "$roles"
        }

  }
}).pretty();

添加了一个$ group,结果我认为结构更好:

    {
        "_id" : ObjectId("556f457594877b4502b2e85d"),
        "nombre" : "app3",
        "roles" : [
                {
                        "id" : ObjectId("556f460b94877b4502b2e867"),
                        "nombre" : "rol7_app3",
                        "activo" : true
                },
                {
                        "id" : ObjectId("556f460194877b4502b2e865"),
                        "nombre" : "rol3_app3",
                        "activo" : true
                }
        ]
}
{
        "_id" : ObjectId("555a33d69487b45401ec1149"),
        "nombre" : "demo",
        "roles" : [
                {
                        "id" : ObjectId("556ca1999487009c1fac125f"),
                        "nombre" : "rol1",
                        "activo" : true
                }
        ]
}

再次感谢@yogesh

1 个答案:

答案 0 :(得分:0)

您的汇总错过了您应该在projectmacthed roles.id == usuarios.roles.id下方匹配的一些事项,请检查以下汇总:

db.collectionName.aggregate({
    "$unwind": "$usuarios"
}, {
    "$match": {
        "usuarios.id": ObjectId("556ca1de9487009c1fac1262")
    }
}, {
    "$unwind": "$usuarios.roles"
}, {
    "$unwind": "$roles"
}, {
    "$project": {
        "nombre": 1,
        "roles": 1,
        "usuarios.id": 1,
        "usuarios.activo": 1,
        "usuarios.roles.id": 1,
        "matched": {
            "$eq": ["$roles.id", "$usuarios.roles.id"] //macthed roles.id == usuarios.roles.id
        }
    }
}, {
    "$match": {
        "matched": true
    }
}).pretty()