mongodb - 如何在项目聚合中删除一个文档
问题描述
我正在使用 mongodb 地图集,我已经建立了一个值。这些是用户收集示例文档的详细信息。
{"_id":{"$oid":"5f7de1f7e0044c8262f6adbc"},"bio":"Hi, I am a professional software developer, I have strong knowledge on coding.","email":"ats4@gmail.com","displayName":"ats demo","following":[],"name":"ats4demo6880904256665157633","image":" ","status":"active","isPrivate":false}
我试图显示除一个之外的所有文档。
"{\"collection\":\"users\",\"stages\":[{\"$match\":{\"$and\":[{\"_id\":{\"$oid\":\"%%args.user\"}},{\"following\":{\"$size\":0}}]}},{\"$lookup\":{\"from\":\"users\",\"localField\":\"status\",\"foreignField\":\"status\",\"as\":\"test\"}},{\"$unwind\":\"$test\"},{\"$project\":{\"test._id\":1,\"test.name\":1,\"test.displayName\":1,\"test.image\":1}}]}"
我已经在项目聚合中显示了除 test._id: ObjectId("5f7f193585d5f70c177f6d27") 之外的所有值。但我不能。
你能帮我解决一下吗?提前致谢。
解决方案
我花了一段时间才明白这个问题。我想这就是你要找的。
db.collection.aggregate([
{
"$match": {
"$and": [
{
"_id": {
"$oid": "5f7de1f7e0044c8262f6adbc"
}
},
{
"following": {
"$size": 0
}
}
]
}
},
{
"$lookup": {
"from": "collection",
"localField": "status",
"foreignField": "status",
"as": "test"
}
},
{
"$unwind": "$test"
},
{
"$match": {
"test._id": {
$not: {
$eq: ObjectId("5f7de1f7e0044c8262f6adcc")
}
}
}
},
{
"$project": {
"test._id": 1,
"test.name": 1,
"test.displayName": 1,
"test.image": 1
}
}
])
在这里玩查询
推荐阅读
- scripting - UDEV- 为什么回声不受影响?
- php - 在 Codeigniter 的 form_validation 库中使用 set_value() 函数时面临问题
- firebase - 在firebase云功能中如何获取登录了哪个应用用户
- android - 如何在 android recyclerview 中添加到收藏夹?
- node.js - 运行 npm Start 时我无法解决错误
- google-cloud-platform - GCP IoT 停止处理消息
- amazon-web-services - 保护 Amazon S3 中的资源
- flutter - 使用 Flutter an Hive 缓存 API 结果
- arrays - 以与 C/C++ 类似的方式将数组传递给 Julia 二进制代码
- r - 从 R 中的另一个工作表调用图形