首页 > 解决方案 > 如何在项目聚合中删除一个文档

问题描述

我正在使用 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") 之外的所有值。但我不能。

你能帮我解决一下吗?提前致谢。

标签: mongodbaggregation-framework

解决方案


我花了一段时间才明白这个问题。我想这就是你要找的。

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
    }
  }
])

在这里玩查询


推荐阅读