MongoDB 4.4 根据结构匹配和分组数组查询

huangapple go评论55阅读模式
英文:

MongoDB 4.4 match with group by array query according to structure

问题

这是您的MongoDB聚合管道查询的翻译,该查询可以满足您的需求:

db.collection.aggregate([
  {
    $match: {
      contentType: "Json",
      code: { $in: [200, 404] } // Filter by status codes 200 and 404
    }
  },
  {
    $group: {
      _id: {
        code: "$code", // Group by status code
        fieldName: "$fieldName",
        location: "$location"
      },
      values: {
        $push: "$value"
      }
    }
  },
  {
    $group: {
      _id: "$_id.code", // Group by status code again
      samples: {
        $push: {
          location: "$_id.location",
          field: "$_id.fieldName",
          values: "$values"
        }
      }
    }
  },
  {
    $project: {
      _id: 0,
      code: "$_id",
      samples: {
        $slice: ["$samples", 5] // Limit to the first 5 samples
      }
    }
  },
  {
    $group: {
      _id: null,
      data: {
        $push: "$$ROOT"
      }
    }
  },
  {
    $project: {
      _id: 0,
      data: 1
    }
  }
])

这个聚合管道查询将根据您的要求对文档进行过滤、分组和限制,并按您所需的结构返回结果。

英文:

Working on mongodb 4.4 version Lets say my collection looks like:

  [
  {
    "_id": ObjectId("6451529ad254d565bd12156b"),
    "contentType": "Json",
    "fieldName": "xxx",
    "method": "GET",
    "value": "Car",
    "location": "header",
    "code": 404
  },
  {
    "_id": ObjectId("6451529ad254d565bd12156d"),
    "contentType": "Json",
    "fieldName": "xxx",
    "method": "GET",
    "value": "Plane",
    "location": "header",
    "code": 404
  },
  {
    "_id": ObjectId("6451529ad254d565bd121570"),
    "contentType": "Json",
    "fieldName": "yyy",
    "method": "GET",
    "value": "Smile",
    "location": "header",
    "code": 404
  },
  {
    "_id": ObjectId("6451529ad254d565bd13156b"),
    "contentType": "Empty",
    "fieldName": "zzz",
    "method": "POST",
    "value": "Train",
    "location": "header",
    "code": 200
  },
  {
    "_id": ObjectId("6451529ad254d5651112156a"),
    "contentType": "Empty",
    "fieldName": "zzz",
    "method": "POST",
    "value": "Player",
    "location": "body",
    "code": 200
  },
  {
    "_id": ObjectId("6451529ad254d565bd12166d"),
    "contentType": "Empty",
    "fieldName": "zzz",
    "method": "POST",
    "value": "Van",
    "location": "body",
    "code": 404
  },
  {
    "_id": ObjectId("6451529ad254d565bd121670"),
    "contentType": "Json",
    "fieldName": "name4",
    "method": "GET",
    "value": "Van",
    "location": "body",
    "code": 200
  }
]

I'd like to request mongo with

{ contentType: Json   codes:  [ 200, 404  ] }

for example i want to filter all documents with contentType:Json and codes 200,404
and group by fieldName with limit 5

the response of this request should look like :

{
  data: [
    {
      code: 200,
      samples: [
        {
          location: “header”,
          “field”: fieldName1,
          values: [
            x1,
            x2, // limit 5
          ]
        },
      {
        location: “headers”,
        field: fieldName2,
        values: [
          y1,
          y2,
          y3 // .. limit 5
      	]
      }
    } ,
    
    {
      code: 404,
      samples: [
        {
          location: “body”,
          “field”: fieldName1,
          values: [
            x1,
            x2 // .. limit 5
          ]
        },
      {
        location: “headers”,
        field: fieldName2,
        values: [
          y1,
          y2,
          y3 .. // limit 5
      	]
      }
    }
  ]
}

tried to do like this but it is not completed https://mongoplayground.net/p/sPk2lViV26l

 db.collection.aggregate([
  {
    $match: {
      contentType: "Json"
    }
  },
  {
    $group: {
      _id: {
        fieldName: "$fieldName",
        location: "$location"
      },
      values: {
        $push: "$value"
      }
    }
  },
  {
    $replaceRoot: {
      newRoot: {
        $mergeObjects: [
          "$_id",
          {
            values: "$values"
          }
        ]
      }
    }
  }
])

which gives me

[
  {
    "fieldName": "xxx",
    "location": "header",
    "values": [
      "Car",
      "Plane"
    ]
  },
  {
    "fieldName": "name4",
    "location": "body",
    "values": [
      "Van"
    ]
  },
  {
    "fieldName": "yyy",
    "location": "header",
    "values": [
      "Smile"
    ]
  }
]

but I need it as array with status code as in the response structure

https://mongoplayground.net/p/sPk2lViV26l

答案1

得分: 1

以下是您要翻译的内容:

"我对所需的分组不是完全清楚,但根据我的理解,以下是一种方法。"

db.collection.aggregate([
  {
    "$match": {
      "contentType": "Json",
      "code": {"$in": [200, 404]}
    }
  },
  {
    "$group": {
      "_id": {
        "code": "$code",
        "field": "$fieldName",
        "location": "$location"
      },
      "values": {"$push": "$value"}
    }
  },
  {
    "$group": {
      "_id": "$_id.code",
      "samples": {
        "$push": {
          "field": "$_id.field",
          "location": "$_id.location",
          "values": {"$slice": ["$values", 5]}
        }
      }
    }
  },
  {
    "$group": {
      "_id": null,
      "data": {
        "$push": {
          "code": "$_id",
          "samples": "$samples"
        }
      }
    }
  },
  {"$unset": "_id"}
])

mongoplayground.net上尝试它。

英文:

The desired grouping isn't completely clear to me, but using my interpretation, here's one way to do it.

db.collection.aggregate([
  {
    "$match": {
      "contentType": "Json",
      "code": {"$in": [200, 404]}
    }
  },
  {
    "$group": {
      "_id": {
        "code": "$code",
        "field": "$fieldName",
        "location": "$location"
      },
      "values": {"$push": "$value"}
    }
  },
  {
    "$group": {
      "_id": "$_id.code",
      "samples": {
        "$push": {
          "field": "$_id.field",
          "location": "$_id.location",
          "values": {"$slice": ["$values", 5]}
        }
      }
    }
  },
  {
    "$group": {
      "_id": null,
      "data": {
        "$push": {
          "code": "$_id",
          "samples": "$samples"
        }
      }
    }
  },
  {"$unset": "_id"}
])

Try it on [mongoplayground.net](https://mongoplayground.net/p/Jyiq11sS1es "Click me!").

huangapple
  • 本文由 发表于 2023年5月10日 15:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定