使用MongoTemplate聚合

时间:2019-02-01 18:20:41

标签: mongodb spring-boot aggregation-framework mongotemplate

我正在尝试过滤其标题与给定的输入标题数组匹配的字段数组并显示不匹配字段的完整文档。我有以下文档。实际上,我想使用MongoTemplate来实现这一点,但首先我想通过mongo查询来获取它。以下是我的文档:

{
    "version": 2,
    "pageName": "Content_2",
    "domain": "bingo.com",
    "locale": "en-us",
    "contents": [
        {
            "contentName": "Template_2",
            "fields": [
                {
                    "fieldType": "Plain Text",
                    "id": "companyName456",
                    "title": "Company Name",
                    "alternateText": "Company Name",
                    "value": "Microsoft",
                    "placeholder": "Enter your Company name"
                },
                {
                    "fieldType": "Plain Text",
                    "id": "designation789",
                    "title": "Designation",
                    "alternateText": "Designation",
                    "value": "Software Engineer",
                    "placeholder": "Enter your designation name"
                }
            ]
        }
    ]
}

我在下面的查询中尝试过,但是返回空结果:

db.contents.aggregate(
  [
    { $match: { locale: "en-us" } },
    {
      $redact: {
        $cond: {
          if: { $in: [ "$title", ["Designation"] ] },
          then: "$$DESCEND",
          else: "$$PRUNE"
        }
      }
    }
  ]
);

我期望得到以下结果:

{
    "pageName": "Home",
    "link": "hello.com",
    "locale": "en-us",
    "contents": [
        {
            "contentName": "Template_2",
            "fields": [
                {
                    "fieldType": "Plain Text",
                    "id": "designation789",
                    "title": "Designation",
                    "alternateText": "Designation",
                    "value": "Software Engineer",
                    "placeholder": "Enter your designation name"
                }
            ]
        }
    ]
}

请指导。我对MongoDB很陌生。

1 个答案:

答案 0 :(得分:1)

类似的事情应该起作用

db.contents.aggregate([
# Match that local
     {$match: { locale: "en-us" } },
# Unwind by contents and contents.fields
     {$unwind: "$contents"},
     {$unwind: "$contents.fields"},
# Match them
     {$match: { "contents.fields.title": "Designation" } },
# group back by _id of the document
     {$group: {
         "_id": "$_id",
         "contents": { "$push": "$contents" }
     }}
 ])

如果您还需要所有其他字段分组,例如

db.contents.aggregate([
     {$match: { locale: "en-us" } },
     {$unwind: "$contents"},
     {$unwind: "$contents.fields"},
     {$match: { "contents.fields.title": "Designation" } },
     {$group: {
         "_id": {
            "_id": "$_id",
            "version": "$version",
            "pageName": "$pageName",
            "domain": "$domain",
            "locale": "$locale",

        },
         "contents": { "$push": "$contents" }
     }},
     {$project: {"version": "$_id.version", 
                 "_id": "$_id._id",
                 "pageName": "$_id.pageName",
                 "locale": "$_id.locale",
                 "domain": "$_id.domain",
                 "contents": "$contents",}}
 ])