【问题标题】:Filter nested elements in MongoDB using MongoTemplate Aggregation使用 MongoTemplate 聚合过滤 MongoDB 中的嵌套元素
【发布时间】:2019-06-26 08:41:19
【问题描述】:

我正在尝试过滤标题与给定输入标题数组匹配的字段数组,并显示不匹配字段的完整文档。我有以下文档。实际上,我想使用 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 很陌生。

【问题讨论】:

    标签: mongodb spring-boot aggregation-framework mongotemplate


    【解决方案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",}}
     ])
    

    【讨论】:

    • 谢谢。它帮助了我。在 $project 内部,是否有更短的方法来投影所有外部字段?我已经尝试过这种方法,但我正在寻找更短的解决方案。
    • 据我所知,我也是 mongo 的新手
    • 我能够使用以下查询实现我的结果:db.contents.aggregate([ {$match: { pageName: "Content_2"}}, {$unwind: "$contents"}, {$unwind: "$contents.fields"}, {$match: { "contents.fields.title": {$in: ["Company Name"]}}}, {$group: { "_id": "$_id", "contents": { "$push": "$contents" }, "root":{$first:"$$ROOT"} }}, {$replaceRoot:{newRoot:{$mergeObjects:["$root",{contents:'$contents'}]}}} ]);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2020-08-19
    • 2019-04-12
    • 2015-06-07
    • 2018-01-30
    • 2014-12-31
    • 1970-01-01
    相关资源
    最近更新 更多