mongodb - 猫鼬查询或聚合以删除某些子文档字段
问题描述
说我有以下文件
物品
{
details: [
{
"title": "Name",
"content": "",
"is_private": false,
"order": 0,
"type": "text"
},
{
"title": "Price",
"content": "",
"is_private": true,
"order": 1,
"type": "text"
},
{
"title": "Company",
"content": "",
"is_private": false,
"order": 2,
"type": "text"
}
],
}
如果我只想返回具有的子文档字段details
,有is_private === false
没有办法在猫鼬的查询中做到这一点,或者我是否需要使用聚合?
例如
ItemModel
.find()
// something should go here to remove
解决方案
如果您想使用本机聚合查询来执行此操作:
db.getCollection('item').aggregate([
{
$unwind:"$details"
},
{
$match:{
"details.is_private":true
}
},
{
$group:{
_id:"$_id",
details:{
$push:"$details"
}
}
}
])
输出:
{
"_id" : ObjectId("5b7d0edb6cfaf771ecf675f0"),
"details" : [
{
"title" : "Price",
"content" : "",
"is_private" : true,
"order" : 1.0,
"type" : "text"
}
]
}
推荐阅读
- python - curl cmd 命令返回内部服务器错误
- javascript - 在多个链接上绑定 onlick 事件
- node.js - 弹性 APM HTTP 错误 (404):找不到 404 页面
- javascript - Node.js中如何根据特定条件对数组中的对象进行分组,使它们不是同一个对象?
- numpy - 从 anaconda 环境运行 python 和 numpy 1.18 的 Gitlab CI 运行程序
- json - 使用 PostgreSQL 从 json 中提取数据的问题
- reactjs - 如何使用 aws cognito 联合身份处理重定向
- anylogic - 未解决:从具有动态事件的等待块中释放给定数量的托盘
- java - 如何将地图转换为 DTO
- java - 生成在终端中写入内存的随机代码