如何使用它过滤地图';s';键';在mongodb

如何使用它过滤地图';s';键';在mongodb,mongodb,aggregation-framework,Mongodb,Aggregation Framework,图中显示了存储在mongodb中的文档- ,文档看起来像: { "_id": ObjectId("5e17d0d13cf7e611a212d797"), "tipset_height": NumberLong(101), "total_block_count": NumberLong(273), "tipset_block_count": NumberLong(3), "time_stamp": NumberLong(1576089945), "c

图中显示了存储在mongodb中的文档- ,文档看起来像:

{
    "_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"
    }
}}
谢谢您的帮助。

试试这个:

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

应该是:{$match:{$or:[{'bson“miners.t0555':{$exists:true}},{'bson“miners.t0888':{$exists:true}]}},@user12686997:不确定您在哪里执行此查询?