1

私のMongoに次のドキュメントがあり、指定されたIDを持つオブジェクトを取得しようとしています。これが私のMongoドキュメントです。モンゴのバージョン: 2.6

{
    "_id" : ObjectId("57c1ae9ac1bd31d4eb4d546d"),
    "footers" : [ 
        {
            "type" : "web",
            "rows" : [ 
                {
                    "id" : "abc",
                    "elements" : [ 
                        {
                            "id" : "def",
                            "type" : "image",
                            "url" : "http://example.com"
                        }, 
                        {
                            "id" : "ghi",
                            "type" : "image",
                            "url" : "http://example.com"
                        }
                    ]
                }
            ]
        }
    ]
}

IDが「def」のオブジェクトを探していて、この結果を取得したい:

{
    "id" : "def",
    "type" : "image",
    "url" : "http://example.com"
}

以下に、このオブジェクトを検索しようとしたコードの例を引用します。

db.getCollection('myCollection').aggregate([
    {"$match": {
        "footers.rows.elements.id": "def"
    }},
    {"$group": {
        "_id": "$footers.rows.elements"
    }}
])

結果は次のとおりです。

{
    "_id" : [ 
        [ 
            [ 
                {
                    "id" : "def",
                    "type" : "image",
                    "url" : "http://example.com"
                }, 
                {
                    "id" : "ghi",
                    "type" : "image",
                    "url" : "http://example.com"
                }
            ]
        ]
    ]
}

助言がありますか?

4

1 に答える 1