使用Join从Mongo数据库集合中获取数据

时间:2020-06-02 11:22:12

标签: mongodb

我是Mongo Database的新手,有人可以帮助我根据我的两个集合结构找到获取详细信息的方法

我有两个集合collection1和collectiond2,我只想从collection2中获得在collection1中具有引用的对象

您能帮我个忙吗

收藏1

{
    "_id": {
        "$oid": "5bb899b1d2c457823df1dxyz"
    },
    "top_element": {
        "_ref": {
            "$ref": "collection2",
            "$id": "top_element_123"
        }
    },
    "bottom_element": {
        "_ref": {
            "$ref": "collection2",
            "$id": "bottom_element_123"
        }
    }
} 

收藏2

{
    "_id": "top_element_123",
    "parent": "parent1",
    "child": "child1"
},
{
    "_id": "bottom_element_123",
    "parent": "parent1",
    "child": "child2",
},
{
    "_id": "top_element_456",
    "parent": "parent2",
    "child": "child2",
},
{
    "_id": "top_element_456",
    "parent": "parent2",
    "child": "child3",
}

我的预期输出

{
    "_id": "top_element_123",
    "parent": "parent1",
    "child": "child1"
},
{
    "_id": "bottom_element_123",
    "parent": "parent1",
    "child": "child2",
}

0 个答案:

没有答案