首页 > 解决方案 > 我如何在 mongodb 中使用它的“键”过滤地图

问题描述

图像显示存储在 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"
    }
}}

谢谢你的帮助..

标签: mongodbaggregation-framework

解决方案


试试这个 :

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'] }] }
                    }
                }
            }
       }
 }])

推荐阅读