如何在mongodb中查找数组中的字段

时间:2017-05-24 07:06:37

标签: mongodb mongodb-query aggregation-framework

String imageSource = myactivity.getResources().getString(R.string.changUrla);

我想要做的是,用文档替换id字段。

{
    "id": "1",
    "label" : "alex",
    "children" : [
        {
            "id" : "rJqS8aW-W"
        },
        {
            "id" : "SkF8UaW-W"
        }
    ],
}
{
    "label" : "felix",
    "id" : "rJqS8aW-W",
    "children" : [
        {
            "id" : "S1gcBUT---"
        },
        {
            "id" : "r1ltUUpZbb"
        }
    ]
}
{
    "label" : "tom",
    "id" : "SkF8UaW-W",
    "children" : [
        {
            "id" : "S1gcBUT---"
        },
        {
            "id" : "r1ltUUpZbb"
        }
    ]
}
....

为了能够做到这一点,我写道;

{
    "id": "1",
    "label" : "alex",
    "children" : [
        {
            "id" : "rJqS8aW-W"
        },
        {
            "id" : "SkF8UaW-W"
        }
    ],
    details: [
        {
            "label" : "felix",
            "id" : "rJqS8aW-W",
            "children" : [
                {
                    "id" : "S1gcBUT---"
                },
                {
                    "id" : "r1ltUUpZbb"
                }
            ]
        },
        {
            "label" : "tom",
            "id" : "SkF8UaW-W",
            "children" : [
                {
                    "id" : "S1gcBUT---"
                },
                {
                    "id" : "r1ltUUpZbb"
                }
            ]
        }

    ]
}
...

但它并没有像我预期的那样回归。我们不能对数组使用db.collection.aggregate([ {$lookup: {from: "collection" , localField: "children.id", foreignField: "id", as: "details"}} ]) 吗?我的意思是我想根据数组中的字段进行搜索。

编辑:

一些修改,例如,

$lookup

 db.collection.aggregate([
        {$lookup: {from: "collection" , localField: "$children.id", foreignField: "id", as: "details"}}
    ])

也会引发错误。

我该怎么办? db.collection.aggregate([ {$lookup: {from: "collection" , localField: "children.$.id", foreignField: "id", as: "details"}} ]) ID字段是flat而不是children

0 个答案:

没有答案