mongodb - 我如何在 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"
}
}}
谢谢你的帮助..
解决方案
试试这个 :
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'] }] }
}
}
}
}
}])
推荐阅读
- filter - 在 Tableau 上隐藏整个筛选器
- r - 离散随机变量的“一个接一个”实现
- python - ResolvePackageNotFound 错误:创建 Conda 环境时
- java - Android 错误:无法找到明确的活动类
- c - 具有可变长度的字符串的数组问题(可能未初始化可变大小的对象)
- three.js - 如何在反应三纤维中渲染另一个物体的发射材料的反射?
- image - 为什么我的所有图像都显示为问号?
- javascript - HTML/JS - 在页面加载时播放音频文件
- wordpress - Wordpress 登录重定向循环
- r - 在 R 中添加不同的时间段(创建新的总年龄列)