как я могу отфильтровать карту по ключу в mongodb - PullRequest
0 голосов
/ 10 января 2020

Изображение показывает документ, хранящийся в mongodb - document , Документы выглядят так:

{
    "_id": ObjectId("5e17d0d13cf7e611a212d797"),
    "tipset_height": NumberLong(101),
    "total_block_count": NumberLong(273),
    "tipset_block_count": NumberLong(3),
    "time_stamp": NumberLong(1576089945),
    "current_tipset_rewards": "131876527590796687386",
    "chain_released_rewards": "12000799601617437127764",
    "miners": {
        "t0222": {
            "miner": "t0222",
            "mined_block_count": NumberLong(1),
            "rewards": "43958842530265562462"
        },
        "t0555": {
            "miner": "t0555",
            "mined_block_count": NumberLong(1),
            "rewards": "43958842530265562462"
        }
    }
} {
    "_id": ObjectId("5e17d0d13cf7e611a212d7f4"),
    "tipset_height": NumberLong(102),
    "total_block_count": NumberLong(276),
    "tipset_block_count": NumberLong(3),
    "time_stamp": NumberLong(1576089990),
    "current_tipset_rewards": "131876518895035818024",
    "chain_released_rewards": "12132676120512472945788",
    "miners": {
        "t0333": {
            "miner": "t0333",
            "mined_block_count": NumberLong(1),
            "rewards": "43958839631678606008"
        },
        "t0444": {
            "miner": "t0444",
            "mined_block_count": NumberLong(1),
            "rewards": "43958839631678606008"
        },
    }
} {
    "_id": ObjectId("5e17d0d13cf7e611a212d79b"),
    "tipset_height": NumberLong(106),
    "total_block_count": NumberLong(287),
    "tipset_block_count": NumberLong(2),
    "time_stamp": NumberLong(1576090170),
    "current_tipset_rewards": "87917656074665382964",
    "chain_released_rewards": "12616223281097686300522",
    "miners": {
        "t0444": {
            "miner": "t0444",
            "mined_block_count": NumberLong(1),
            "rewards": "43958828037332691482"
        },
        "t0555": {
            "miner": "t0555",
            "mined_block_count": NumberLong(1),
            "rewards": "43958828037332691482"
        }
    }
}

, и я хочу запрос, который дает результаты, в которых miners имеет ключ в [ "t0888", "t0555"], и поскольку в вышеприведенных документах нет 't0888', результат выглядит следующим образом:

{
"_id": ObjectId("5e17d0d13cf7e611a212d797"),
"tipset_height": NumberLong(101),
"total_block_count": NumberLong(273),
"tipset_block_count": NumberLong(3),
"time_stamp": NumberLong(1576089945),
"current_tipset_rewards": "131876527590796687386",
"chain_released_rewards": "12000799601617437127764",
"miners": {
    "t0555": {
        "miner": "t0555",
        "mined_block_count": NumberLong(1),
        "rewards": "43958842530265562462"
    }
}},{
"_id": ObjectId("5e17d0d13cf7e611a212d79b"),
"tipset_height": NumberLong(106),
"total_block_count": NumberLong(287),
"tipset_block_count": NumberLong(2),
"time_stamp": NumberLong(1576090170),
"current_tipset_rewards": "87917656074665382964",
"chain_released_rewards": "12616223281097686300522",
"miners": {
    "t0555": {
        "miner": "t0555",
        "mined_block_count": NumberLong(1),
        "rewards": "43958828037332691482"
    }
}}

спасибо за любую помощь ..

1 Ответ

0 голосов
/ 10 января 2020

Попробуйте это:

db.yourCollectionName.aggregate([
    // $match as initial stage to filter the docs as we're doing this op on entire collection (Optional on small dataset)
    { $match: { $or: [{ 'miners.t0555': { $exists: true } }, { 'miners.t0888': { $exists: true } }] } },
    /** Converting miner to array to iterate over and keep only needed keys & later converting back from array to object,
       $addFields will replace miners with result of this stage */
    {
        $addFields: {
            miners: {
                $arrayToObject: {
                    $filter: {
                        input: { $objectToArray: "$miners" },
                        as: "item",
                        cond: { $or: [{ $eq: ["$$item.k", 't0555'] }, { $eq: ["$$item.k", 't0888'] }] }
                    }
                }
            }
       }
 }])
...