mongodb - 将条件推入数组
问题描述
我们只需要push/addtoset 文件中的key ne []
怎么实现
{
"_id" : ObjectId("xxxxxx"),
"shop" : "REQ4",
"bolt" : "5647",
"nut" : "1111",
}
{
"_id" : ObjectId("xxxxxx"),
"shop" : "REQ4",
"bolt" : "2314",
"nut":[]
}
Aggregates.group("$shop", Accumulators.addToSet("bolt", "$bolt"),Accumulators.addToSet("nut", "nut"))//only if nut ne []
预期输出:
{ "_id" : "REQ4", "bolt" : ["5647", "2314"], "nut" : ["1111"]
解决方案
你可以先$push
然后可以用$filter
来排除[]
db.collection.aggregate([
{ "$group": {
"_id": "$shop",
"bolt": { "$push": "$bolt" },
"nut": { "$push": "$nut" }
}},
{ "$addFields": {
"nut": {
"$filter": {
"input": "$nut",
"cond": { "$ne": ["$$this", []] }
}
}
}}
])
推荐阅读
- amazon-s3 - 在不同的 IAM 角色下运行 AWS Lambda 函数
- r - 在使用 R 中的 igraph 生成 k-正则图时,每个 k-正则图是否都是唯一且非随机的?
- ios - 将 Timer 添加到主 RunLoop 是正确的解决方案吗?
- java - 单击后获取警报对话框项目列表以显示在不同的片段、适配器或活动中
- mysql - 仅用于配置数据的名称-值-对模型
- mysql - phpmyadmin:为什么导出不保存到指定的 SaveDir?
- python-3.x - 'numpy.ndarray' 对象不可调用'for i'
- python - pandas concat 函数如何在不指定公共键的情况下进行列连接?
- android - 为什么建议不要对 Android 房间使用 allowMainThreadQueries()?
- python - Python:从另一个函数参数内部调用函数