mongodb - MongoDB聚合将列表转换为布尔值
问题描述
我需要在 MongoDB 中进行查询,根据列表中是否存在值,我将列表转换为 True 或 False。
这是架构的总结视图。
{'_id': ObjectId('604edf91571e7ab19cc2c238'),
'timestamp': 1616164767.773929,
'name': 'some name',
'metric1': 1,
'metric2': 2,
'metric3': 3,
'tags': ['tag1', 'tag2', 'tag3', 'tag that matters']}
'tag that matters'
如果 a在列表中,有没有办法将 tags 键更改为 True 或 False 的值?我不想排除没有标签的记录,我只想让这些记录的标签键值为 False。
解决方案
试试这个查询:
db.collectionName.aggregate([
{
"$addFields": {
"tags": {
$map: {
input: "$tags",
as: "tag",
in: {
$eq: ['$$tag', 'tag that matters']
}
}
}
}
}
]);
输出:
{
"_id" : ObjectId("604edf91571e7ab19cc2c238"),
"timestamp" : 1616164767.773929,
"name" : "some name",
"metric1" : 1,
"metric2" : 2,
"metric3" : 3,
"tags" : [
false,
false,
false,
true
]
}
推荐阅读
- python - 提取 1 列中的数值并将其添加到已解析索引下的新索引中
- javascript - 使用多个 async/await 调用构建对象
- java - 在方法中获取没有 Arrayname 的 JSONArray
- javascript - Selenium:通过 Javascript 抓取所有元素属性返回不正确的结果
- linux - Ansible sudo 默认提示
- docker - 从可移动媒体运行 Docker 容器
- c++ - 从 C++ 中的函数返回对象:它的过程和性质
- javascript - 生产错误中的节点集群
- c# - DynamicData显示多条折线图
- javascript - 当列标题卡在页面顶部时如何对齐它们