python - 删除 pyMongo 列表中的列表条目
问题描述
我有一个数据库集合,其中包含这样的对象:
{
"_id": ObjectId("something"),
"name_lower": "total",
"name": "Total",
"mounts": [
[
"mount1",
"instance1"
],
[
"mount2",
"instance1"
],
[
"mount1",
"instance2"
],
[
"mount2",
"instance2"
]
]
}
假设我想删除具有实例 instance2 的每个挂载,我将如何去做?我已经搜索了很长时间。
解决方案
你可以做这样的事情
[
{
$unwind: "$mounts"
},
{
$match: {
"mounts": {
$ne: "instance2"
}
}
},
{
$group: {
_id: "$_id",
name: {
$first: "$name"
},
mounts: {
$push: "$mounts"
}
}
}
]
工作Mongo游乐场
推荐阅读
- algorithm - 优化 - 将参与者分散在远离彼此的地方
- appium - 除了 scrollTo、Touchoption、Swipe 之外的 appium 版本 1.7 的向下滚动脚本
- java - 超出每个用户的会话限制
- php - 如何将 laravel ..env 变量包含到 js 文件中
- python - 在部署 Azure ML 试验服务时包括其他脚本
- qt - qmake:每个 make 调用的自定义步骤
- python - pywifi不能安装在python上
- .net - EF fluent 映射 DbGeography 问题:类型“System.Data.Spatial.DbGeography”必须是不可为空的值类型才能将其用作参数“T”
- datetime - DateTime 的国际化不起作用
- python - 无法识别使用 **kwarg 格式的任意关键字位置参数