1

次のポートフォリオコレクションを想定しています。

{
    "_id" : ObjectId("50e5a858ad06fe3439000001"),
    "name" : "Portfolio 1",
    "description" : "description Portfolio 1",
    "userId" : "",
    "wallets" : [ ]
}
{
    "_id" : ObjectId("50e5a858ad06fe3439000002"),
    "name" : "Portfolio 2",
    "description" : "description Portfolio 2",
    "userId" : "",
    "wallets" : [
        {
            "_id" : ObjectId("50e5ac69f214a46139000001"),
            "name" : "wallet name 2-1",
            "description" : "description du wallet",
        "cards" : [
            {
                "_id" : ObjectId("50ebe4b5906a1e830d000001"),
                "name" : "Card 2-1-1",
                "description" : "description card 1",
            },
            {
                "_id" : ObjectId("50ebe61f2c0f189310000001"),
                "name" : "Card 2-1-2",
                "description" : "description de la carte",
            },
            {
                "_id" : ObjectId("50ebe6202c0f189310000002"),
                "name" : "Card 2-1-2",
                "description" : "description de la carte",
            },
            {
                "_id" : ObjectId("50ebe6212c0f189310000003"),
                "name" : "Card 2-1-3",
                "description" : "description de la carte",
            },
            {
                "_id" : ObjectId("50ebe6212c0f189310000004"),
                "name" : "Card 2-1-4",
                "description" : "description de la carte",
            }
        ]
    },
    {
        "_id" : ObjectId("50ebe6b22c0f189310000005"),
        "name" : "wallet 2-2",
        "description" : "",
        "cards" : [
            {
                "_id" : ObjectId("50ec21063f3c5f9f12000001"),
                "name" : "Card 2-2-1",
                "description" : "",
            }
        ]
    },
    {
        "_id" : ObjectId("50ebe6ba2c0f189310000006"),
        "name" : "wallet 2-3",
        "description" : "",
        "cards" : [
            {
                "_id" : ObjectId("50ec21233f3c5f9f12000002"),
                "name" : "Card 2-3-1",
                "description" : "",
            }
        ]
    }
]
}

ポートフォリオ、ウォレット、カード_idのおかげで、特定のカードにアクセスしたいと思います。私が欲しいのは:

{
"_id" : ObjectId("50e5a858ad06fe3439000002"),
"wallets" : [
    {
        "_id" : ObjectId("50e5ac69f214a46139000001"),
        "cards" : [
            {
                "_id" : ObjectId("50ebe4b5906a1e830d000001"),
                "name" : "Card 2-1-1",
                "description" : "description card 1",
            }
        ]
    },
]

}

nodejs/mongo-native-driverでmongodb2.2.2に取り組んでいます。

どんな助けでも感謝されます(シェルの例またはjavascriptが感謝されます)

ありがとう。

4

3 に答える 3

3

Aggregation Frameworkを使用した slee の回答に似ていますが、不要な巻き戻しを防ぐために、結果からエントリを早期に選別することをお勧めします。

    var aggOps = [
        { $match: {
            _id: portfolioID
        }},
        {$unwind: '$wallets'},
        { $match: {
            'wallets._id': walletID
        }},
        {$unwind: '$wallets.cards'},
        { $match: {
            'wallets.cards._id': cardID
        }},
    ];

    collection.aggregate(aggOps, function (err, result) {
        console.log(result);
    });
于 2013-01-08T19:23:38.740 に答える
1

http://docs.mongodb.org/manual/reference/projection/elemMatch/

mongodb ドキュメントによると、これは $elemMatch オペレーターによって実行できます。クエリを作成しようとしましたが、それがあなたのケースで機能するかどうかを確認できますか、それとも必要に応じて変更できますか.

var projection = { _id: 1, wallets: { _id: 'walletId', $elemMatch: { cards._id: 'cardId'}}};

db.portfolio.find(    {_id: 'portfolio_id'},    projection )
于 2013-01-08T15:41:59.597 に答える
1

集約フレームワークを使用できます。具体的には、この場合、おそらく$unwind演算子が最も役立ちます。$unwind は空の配列を展開しないため、例の最初のドキュメントは集計パイプラインから除外されることに注意してください。

db.collection.aggregate(
    { $unwind : "$wallets" },
    { $unwind : "$wallets.cards" },
    { $match: {
        "_id"               : ObjectId("50e5a858ad06fe3439000002"),
        "wallets._id"       : ObjectId("50e5ac69f214a46139000001"),
        "wallets.cards._id" : ObjectId("50ebe4b5906a1e830d000001")
    } }
)
于 2013-01-08T15:45:04.403 に答える