【问题标题】:How can i find the index of an array element that is a dictionary based on one of the keys of the dictionary using mongo query?如何使用 mongo 查询根据字典的键之一找到作为字典的数组元素的索引?
【发布时间】:2020-01-16 08:51:51
【问题描述】:
"_id" : ObjectId("5de64d7802a3414fbf374e75"),
"sectionDTO" : {
    "sectionData" : [
            {
                "type" : "PRODUCT",
                "title" : "<strong>What is Proburst BCAA supreme?</strong>",
                "description" : "<p>Proburst® BCAA Supreme is a power-packed BCAA formula, designed to keep you fit and active. A combination of three essential amino acids, namely Leucine, Isoleucine and Valine, commonly known as “Branched Chain Amino Acids or BCAAs”, along with L-Glutamine.</p>"
            },
            {
                "type" : "PRODUCT",
                "title" : "<strong>Why Proburst BCAA supreme?</strong>",
                "description" : "<p>Proburst® BCAA Supreme is not a regular BCAA formula. It is packed with 7g of BCAAs and 2.5g of L-Glutamine, along with key ingredients like Grape seed Extract and Black Pepper Extract to keep you active all day long.</p>"
            },
            {
                "type" : "PRODUCT_PACKAGE",
                "title" : "<strong>Select your Product:</strong>",
                "description" : "",
                "itemsPerRow" : "2",
                "mItemsPerRow" : "1",
                "comboPackageDetails" : [
                    {
                        "productName" : "Proburst BCAA Powder (250gms)",
                        "packageCode" : "bcaa-mango-flavoured-powder",
                        "productImage" : "https://assets.lybrate.com/q_auto,f_auto/rio/proburst/5/1.jpg"
                    },
                    {
                        "productName" : "Proburst BCAA Powder (400gms)",
                        "packageCode" : "bcaa-mango-flavoured-powder-v1",
                        "productImage" : "https://assets.lybrate.com/q_auto,f_auto/rio/proburst/6/1.jpg"
                    }
                ]
            },
            {
                "type" : "PRODUCT",
                "title" : "<strong>Key benefits of Proburst BCAA Supreme</strong>",
                "description" : "<p>1. Reduces fatigue <br> 2. Keeps you active <br> 3. Helps you meet your ideal requirement of essential amino acids <br>4. Maintains electrolyte balance in the body</p>"
            },
            {
                "type" : "PRODUCT",
                "title" : "<strong>What are the key ingredients of BCAA supreme?</strong>",
                "description" : "<p>1. Each serving of Proburst® BCAA Supreme has 7g of BCAAs in the ratio of 2:1:1 and 2.5g of L-Glutamine to keep you active by providing a dose of essential amino acids. <br> <br>2. Proburst® BCAA Supreme contains a combination of Citrulline Malate and Taurine for improved flow of blood in the vessels. <br> <br>3. Proburst® BCAA Supreme contains Electrolytes that will help in maintaining your body’s fluid levels. <br> <br>4. Contains a combination of powerful antioxidant herbs, Grape Seed (Vitis Vinifera) extract and Black Pepper (Piper Nigrum) extract to boost your immunity and improve absorption of nutrients in the body.</p>"
            },
            {
                "type" : "PRODUCT",
                "title" : "<strong>How to consume BCAA supreme?</strong>",
                "description" : "<p>Take 1 scoop (about 13.3g) of Proburst BCAA Supreme in 250-300ml of water.</p>"
            }
]
}

这就是我的集合现在的样子我想找到数组的索引,其中“标题”等于给定“_id”的特定文本。 有没有办法只使用 mongodb?

到目前为止,我已经尝试使用 $indexofArray 函数但没有找到任何运气。

【问题讨论】:

    标签: mongodb nosql aggregation-framework nosql-aggregation


    【解决方案1】:

    您需要使用$unwind 运算符指定includeArrayIndex 参数。

    db.collection.aggregate([
    {
        $unwind: {
          path: "$sectionDTO.sectionData",
          includeArrayIndex: "index"
        }
      },
      {
        $match: {
          "sectionDTO.sectionData.title": "<strong>Why Proburst BCAA supreme?</strong>"
        }
      }
    ])
    

    MongoPlayground


    [
      {
        "_id": ObjectId("5de64d7802a3414fbf374e75"),
        "index": 1,
        "sectionDTO": {
          "sectionData": {
            "description": "\u003cp\u003eProburst® BCAA Supreme is not a regular BCAA formula. It is packed with 7g of BCAAs and 2.5g of L-Glutamine, along with key ingredients like Grape seed Extract and Black Pepper Extract to keep you active all day long.\u003c/p\u003e",
            "title": "\u003cstrong\u003eWhy Proburst BCAA supreme?\u003c/strong\u003e",
            "type": "PRODUCT"
          }
        }
      }
    ]
    

    【讨论】:

      【解决方案2】:

      由于运算符$indexOfArray&lt;array expression&gt; 参数在以下语法中:

      { $indexOfArray: [ <array expression>, <search expression>, <start>, <end> ] }
      

      可以是任何有效的表达式,只要它解析为数组,使用表达式

      "$sectionDTO.sectionData.title"
      

      返回一个仅包含标题的数组,然后您可以搜索并返回匹配的索引:

      var query = "<strong>Select your Product:</strong>";
      db.collection.aggregate([
          { "$addFields": {
              "indexOfTitle": {
                  "$indexOfArray": [
                      "$sectionDTO.sectionData.title",
                      query
                  ]
              }
          } }
      ]);
      

      【讨论】:

        猜你喜欢
        • 2015-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-19
        • 2023-03-03
        • 2022-10-26
        相关资源
        最近更新 更多