0
"_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>"
            }
]
}

This is how my collection looks now i want to find the index of the array where the "title" is equal to a particular text for a given "_id". Is there a way to do it using just mongodb?

Till now i have tried using the $indexofArray function but did not find any luck.

2 Answers2

0

You need to use $unwind operator specifying includeArrayIndex parameter.

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"
      }
    }
  }
]
Valijon
  • 12,667
  • 4
  • 34
  • 67
0

Since the <array expression> argument for the operator $indexOfArray in the following syntax:

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

can be any valid expression as long as it resolves to an array, use the expression

"$sectionDTO.sectionData.title"

which returns an array of just titles which you can then search and return the matched index as:

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