6

I have documents in this structure:

{
   "_id":{
      "$oid":"..."
   },
   "Index":0,
   "KeyIndex":"...",
   "SerialNumber":"...",
   "Tests":[
      {
         "TestName":"...",
         "Status":"...",
         "Steps":[
            {
               "LowLimit":"X",
               "HighLimit":"Y",
               "Result":"Z"
            },
            {
               "LowLimit":"X",
               "HighLimit":"Y",
               "Result":"Z"
            }
         ],
         "Logs":[
            {
               "Type":"Info",
               "Message":"..."
            },
            {
               "Type":"Warning",
               "Message":"..."
            }
         ]
      },
      {
         "TestName":"...",
         "Status":"...",
         "Steps":[
            {
               "LowLimit":"X",
               "HighLimit":"Y",
               "Result":"Z"
            }
         ],
         "Logs":[
            {
               "Type":"Info",
               "Message":"..."
            },
            {
               "Type":"Info",
               "Message":"..."
            }
         ]
      }
   ]
}

As you can see, The Tests property is an array where each object has its own properties, Which 2 of them are arrays them self. I want a stage which removes from all objects which are in the test array the logs property.

The result should be like this:

{
   "_id":{
      "$oid":"..."
   },
   "Index":0,
   "KeyIndex":"...",
   "SerialNumber":"...",
   "Tests":[
      {
         "TestName":"...",
         "Status":"...",
         "Steps":[
            {
               "LowLimit":"X",
               "HighLimit":"Y",
               "Result":"Z"
            },
            {
               "LowLimit":"X",
               "HighLimit":"Y",
               "Result":"Z"
            }
         ]
      },
      {
         "TestName":"...",
         "Status":"...",
         "Steps":[
            {
               "LowLimit":"X",
               "HighLimit":"Y",
               "Result":"Z"
            }
         ]
      }
   ]
}

I am using compass to build the aggregation, I chose $unset stage and tried:

/**
 * fields: The field name(s).
 */
{
  "$Tests.Logs"
}

And:

/**
 * fields: The field name(s).
 */
{
  ["$Tests.Logs"]
}

which says: Stage must be a properly formatted document.

And:

/**
 * fields: The field name(s).
 */
{
  "$Tests.Logs":1
}

which says: $unset specification must be a string or an array .

ATT
  • 921
  • 3
  • 13
  • 30
  • 3
    see [$unset aggregation](https://docs.mongodb.com/manual/reference/operator/aggregation/unset/) stage, you can either just specify one field in string like `"Tests.Logs"` or you can specify multiple in array `["Tests.Logs"]` – turivishal May 02 '21 at 12:47
  • It says: Stage must be a properly formatted document. When I do that – ATT May 02 '21 at 13:00
  • yes this is not formatted `{ "$Tests.Logs":1 }` as per `$unset` syntax just try only `"Tests.Logs"`. remove object – turivishal May 02 '21 at 13:04
  • I also tried your suggestion { "Tests.Logs" }. which says "Stage must be a properly formatted document." – ATT May 02 '21 at 13:07
  • 2
    please read the documentation provided in first comment, just use only string `"Tests.Logs"`, remove everything and don't wrap in object => remove this `{ }`. – turivishal May 02 '21 at 13:08
  • Oh ok. It Works now. Thanks – ATT May 02 '21 at 13:12

0 Answers0