How to perform Greater than Operator in MongoDb Compass Querying inside inner object collection

Viewed 1318

Here is my Json in Mongo DB Compass. I am just querying greater than rating products from each collection.

Note: if I am doing with pageCount it is working fine because that is not inside a collection.

{PageCount:{gte:2}} -- works.

Problem with inner arrays collection of collection if anyone matches it displays all.

When we are doing the below query if anyone of the index have greater than 99 it shows all the values.

{"ProductField.ProductDetailFields.ProductDetailInfo.ProductScore.Rating": {$exists:true,  $ne: null , $gte: 99}}

----- if I perform above query, I am getting this enter image description here output.

How to iterate like foreach kind of things and check the condition in MongoDB querying

enter image description here

{
   "_id":{
      "$oid":"5fc73a7b3fb52d00166554b9"
   },
   "ProductField":{
      "PageCount":2,
      "ProductDetailFields":[
         {
            "PageNumber":1,
      
            "ProductDetailInfo":[
               {
                  "RowIndex":0,
                  "ProductScore":{
                     "Name":"Samsung",
                     "Rating":99
                  },
                 
               },
               {
                  "RowIndex":1,
                  "ProductScore":{
                     "Name":"Nokia",
                     "Rating":96
                  },
                  
               },
                {
                  "RowIndex":2,
                  "ProductScore":{
                     "Name":"Apple",
                     "Rating":80
                  },
                  
                   
               }

            ]
         }
      ]
   }  
},
{
   "_id":{
      "$oid":"5fc73a7b3fb52d0016655450"
   },
   "ProductField":{
      "PageCount":2,
      "ProductDetailFields":[
         {
            "PageNumber":1,
            "ProductDetailInfo":[
               {
                  "RowIndex":0,
                  "ProductScore":{
                     "Name":"Sony",
                     "Rating":93
                  }
               },
               {
                  "RowIndex":1,
                  "ProductScore":{
                     "Name":"OnePlus",
                     "Rating":93
                  }
               },
               {
                  "RowIndex":2,
                  "ProductScore":{
                     "Name":"BlackBerry",
                     "Rating":20
                  }
               }
            ]
         }
      ]
   }
}

@Misky How to run this query execute: enter image description here

While run this query in Mongo Shell - no sql client throws below error. we are using 3.4.9 https://www.nosqlclient.com/demo/ enter image description here

1 Answers

Is this somewhat close to your idea

db.collection.aggregate({
  $addFields: {
    "ProductField.ProductDetailFields": {
      $map: {
        "input": "$ProductField.ProductDetailFields",
        as: "pdf",
        in: {
          $filter: {
            input: {
              $map: {
                "input": "$$pdf.ProductDetailInfo",
                as: "e",
                in: {
                  $cond: [
                    {
                      $gte: [
                        "$$e.ProductScore.Rating",
                        99
                      ]
                    },
                    {
                      $mergeObjects: [
                        "$$e",
                        {
                          PageNumber: "$$pdf.PageNumber"
                        }
                      ]
                    },
                    null
                  ]
                }
              }
            },
            as: "i",
            cond: {
              $ne: [
                "$$i",
                null
              ]
            }
          }
        }
      }
    }
  }
},
{
  $addFields: {
    "ProductField.ProductDetailFields": {
      "$arrayElemAt": [
        "$ProductField.ProductDetailFields",
        0
      ]
    }
  }
})

LIVE VERSION

Related