Mongo Group Document属性由2个字段进行数据

发布于 2025-01-22 06:06:44 字数 1519 浏览 0 评论 0原文

我有一个具有以下结构的文档:

    {
        _id: "some_id",
        ... ,
        properties: [
            {
                "document": "doc1",
                "sheet": "sheet1",
                "property": "property1"
            },
            {
                "document": "doc1",
                "sheet": "sheet2",
                "property": "property2"
            },
            {
                "document": "doc1",
                "sheet": "sheet2",
                "property": "property3"
            },
            {
                "document": "doc2",
                "sheet": "sheet1",
                "property": "property4"
            },
        ]
    }

我想创建一个查询,以查找文档的所有properties,其中id _id ,然后按document 和表格属性值

示例:

    {
      "document": "File 1",
      "result": [
        {
          "sheet": "sheet1",
          "data": [{...}]
        },
        {
          "sheet": "sheet2",
          "data": [{...}]
        },
        {
          "sheet": "sheet3",
          "data": [{...}]
        }
      ]
    }  

如何执行特定文档的属性的分组/聚合?

我不习惯MongoDB,我唯一要做的就是获得properties投影文档,并带有以下查询:

    db.getCollection('myCollection')
        .find({}, {_id:UUID("21e1fd87-6e22-4487-85d5-18e639f9b710"), properties: 1})

I have a document with the following structure:

    {
        _id: "some_id",
        ... ,
        properties: [
            {
                "document": "doc1",
                "sheet": "sheet1",
                "property": "property1"
            },
            {
                "document": "doc1",
                "sheet": "sheet2",
                "property": "property2"
            },
            {
                "document": "doc1",
                "sheet": "sheet2",
                "property": "property3"
            },
            {
                "document": "doc2",
                "sheet": "sheet1",
                "property": "property4"
            },
        ]
    }

I would like to create a query to find all the properties of the document with ID _id, and then group the data by document and sheet property values

Example:

    {
      "document": "File 1",
      "result": [
        {
          "sheet": "sheet1",
          "data": [{...}]
        },
        {
          "sheet": "sheet2",
          "data": [{...}]
        },
        {
          "sheet": "sheet3",
          "data": [{...}]
        }
      ]
    }  

How can I perform the grouping/aggregation of the properties of a certain document?

I am not used to MongoDB, and the only thing I managed to do is to obtain the properties projection document with the following query:

    db.getCollection('myCollection')
        .find({}, {_id:UUID("21e1fd87-6e22-4487-85d5-18e639f9b710"), properties: 1})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女七分熟 2025-01-29 06:06:44

您可以使用聚合框架进行操作:

  • $ match - 用 _id field
  • $ untind - 以解析 properties 数组字段
  • $ group - document 字段字段
  • $ project仅返回仅返回的字段您想要
db.collection.aggregate([
  {
    "$match": {
      "_id": 1
    }
  },
  {
    "$unwind": "$properties"
  },
  {
    "$group": {
      "_id": {
        "document": "$properties.document",
        "sheet": "$properties.sheet"
      },
      "data": {
        "$addToSet": "$properties.property"
      }
    }
  },
  {
    "$project": {
      "_id": 0,
      "document": "$_id.document",
      "result": {
        "sheet": "$_id.sheet",
        "data": "$data"
      }
    }
  }
])

工作示例

You can do it with Aggregation Framework:

  • $match - to filter the document with _id field
  • $unwind - to deconstruct properties array field
  • $group - to group by document and sheet fields
  • $project to return only the fields that you want
db.collection.aggregate([
  {
    "$match": {
      "_id": 1
    }
  },
  {
    "$unwind": "$properties"
  },
  {
    "$group": {
      "_id": {
        "document": "$properties.document",
        "sheet": "$properties.sheet"
      },
      "data": {
        "$addToSet": "$properties.property"
      }
    }
  },
  {
    "$project": {
      "_id": 0,
      "document": "$_id.document",
      "result": {
        "sheet": "$_id.sheet",
        "data": "$data"
      }
    }
  }
])

Working example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文