MongoDB数组聚合创建新文档查询

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

MongoDB array aggregation create new document query

问题

I want to clarify that you'd like a Chinese translation of the code and the text you provided, but without answering the specific translation request in your content. Here is the translated code and text:

我有这个数组集合:

{
    "_id": 154,
    "title": "title1",
    "authors": [ { "name": "John Smith", "registered": true },
                 { "name": "Jane Doe", "registered": true },
               ],
    "tags": [ "tag1", "tag2", "tag3" ]
}

使用mongodb聚合,我想生成以下输出:

{
    "tag": "tag1", 
    "titles": [ "title1", "title2", "title3"], 
    "size": 3
},
{
    "tag": "tag2", 
    "titles": [ "title3", "title4" ], 
    "size": 2
}

每个标签都应该以tag键呈现。

我想找到其他文档中存在的所有标签,以便我可以创建单独的标题数组,最后size应该显示标题的长度。

我正在使用此查询,但未获得所需的输出

db.posts.aggregate([{$match:{"authors":{$all:[{$elemMatch:{registered:true}}]}}},{$unwind:"$tags"},{$project:{tag:"$tags",size:{$size:"$authors"},_id:0}}])

有人能帮我编写所需的查询吗?

我还尝试过

db.posts.aggregate([{$match:{"authors":{$all:[{$elemMatch:{registered:true}}]}}},{$unwind:"$tags"},{$project:{tag:"$tags",size:{$size:"$authors"},_id:0}}])

但是我没有得到所需的结果。

This translation includes the code and text you provided in Chinese.

英文:

I have this array collection with

{
    "_id": 154,
    "title": "title1",
    "authors": [ { "name": "John Smith", "registered": true },
                 { "name": "Jane Doe", "registered": true },
               ],
    "tags": [ "tag1", "tag2", "tag3" ]
}

By using mongodb aggregation, I want to generate output like this

{
    "tag": "tag1", 
    "titles": [ "title1", "title2", "title3"], 
    "size": 3
},
{
    "tag": "tag2", 
    "titles": [ "title3", "title4" ], 
    "size": 2
}

Each tag should present with tag key.

I want to find all the tags which exists in other documents so that I can create separate array of titles, and at the very last size should show the length of the titles.

I am using this query, but I'm not getting the desired output

db.posts.aggregate([{$match:{"authors":{$all:[{$elemMatch:{registered:true}}]}}},{$unwind:"$tags"},{$project:{tag:"$tags",size:{$size:"$authors"},_id:0}}])

Can someone help me write the desired query?

I also tried

db.posts.aggregate([{$match:{"authors":{$all:[{$elemMatch:{registered:true}}]}}},{$unwind:"$tags"},{$project:{tag:"$tags",size:{$size:"$authors"},_id:0}}])

but I'm not getting desired result.

答案1

得分: 0

I think you may need the $group operator. Are you looking for something like this?

db.collection.aggregate([
  {
    $match: {
      "authors": {
        $all: [
          {
            $elemMatch: {
              registered: true
            }
          }
        ]
      }
    }
  },
  {
    $unwind: "$tags"
  },
  {
    $group: {
      _id: "$tags",
      titles: {
        "$addToSet": "$title"
      }
    }
  },
  {
    $project: {
      _id: 0,
      tag: "$_id",
      titles: 1,
      size: {
        $size: "$titles"
      }
    }
  }
])

Given your sample document of:

{
  "_id": 154,
  "title": "title1",
  "authors": [
    {
      "name": "John Smith",
      "registered": true
    },
    {
      "name": "Jane Doe",
      "registered": true
    }
  ],
  "tags": [
    "tag1",
    "tag2",
    "tag3"
  ]
}

The output is:

[
  {
    "size": 1,
    "tag": "tag2",
    "titles": [
      "title1"
    ]
  },
  {
    "size": 1,
    "tag": "tag3",
    "titles": [
      "title1"
    ]
  },
  {
    "size": 1,
    "tag": "tag1",
    "titles": [
      "title1"
    ]
  }
]

If I add another document of:

{
  "_id": 153,
  "title": "title2",
  "authors": [
    {
      "name": "John Smith",
      "registered": true
    },
    {
      "name": "Jane Doe",
      "registered": true
    }
  ],
  "tags": [
    "tag1",
    "tag3",
    "tag4"
  ]
}

Then the output is:

[
  {
    "size": 1,
    "tag": "tag4",
    "titles": [
      "title2"
    ]
  },
  {
    "size": 2,
    "tag": "tag3",
    "titles": [
      "title1",
      "title2"
    ]
  },
  {
    "size": 2,
    "tag": "tag1",
    "titles": [
      "title1",
      "title2"
    ]
  },
  {
    "size": 1,
    "tag": "tag2",
    "titles": [
      "title1"
    ]
  }
]

Playground demonstration here.

英文:

I think you may need the $group operator. Are you looking for something like this?

db.collection.aggregate([
  {
    $match: {
      "authors": {
        $all: [
          {
            $elemMatch: {
              registered: true
            }
          }
        ]
      }
    }
  },
  {
    $unwind: "$tags"
  },
  {
    $group: {
      _id: "$tags",
      titles: {
        "$addToSet": "$title"
      }
    }
  },
  {
    $project: {
      _id: 0,
      tag: "$_id",
      titles: 1,
      size: {
        $size: "$titles"
      }
    }
  }
])

Given your sample document of:

  {
    "_id": 154,
    "title": "title1",
    "authors": [
      {
        "name": "John Smith",
        "registered": true
      },
      {
        "name": "Jane Doe",
        "registered": true
      },
      
    ],
    "tags": [
      "tag1",
      "tag2",
      "tag3"
    ]
  }

The output is:

[
  {
    "size": 1,
    "tag": "tag2",
    "titles": [
      "title1"
    ]
  },
  {
    "size": 1,
    "tag": "tag3",
    "titles": [
      "title1"
    ]
  },
  {
    "size": 1,
    "tag": "tag1",
    "titles": [
      "title1"
    ]
  }
]

If I add another document of:

  {
    "_id": 153,
    "title": "title2",
    "authors": [
      {
        "name": "John Smith",
        "registered": true
      },
      {
        "name": "Jane Doe",
        "registered": true
      },
      
    ],
    "tags": [
      "tag1",
      "tag3",
      "tag4"
    ]
  }

Then the output is:

[
  {
    "size": 1,
    "tag": "tag4",
    "titles": [
      "title2"
    ]
  },
  {
    "size": 2,
    "tag": "tag3",
    "titles": [
      "title1",
      "title2"
    ]
  },
  {
    "size": 2,
    "tag": "tag1",
    "titles": [
      "title1",
      "title2"
    ]
  },
  {
    "size": 1,
    "tag": "tag2",
    "titles": [
      "title1"
    ]
  }
]

Playground demonstration here.

huangapple
  • 本文由 发表于 2023年6月8日 18:48:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431064.html
匿名

发表评论

匿名网友

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

确定