$unset specification must be a string or an array
Asked Answered
S

0

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 .

Salutation answered 2/5, 2021 at 12:27 Comment(6)
see $unset aggregation stage, you can either just specify one field in string like "Tests.Logs" or you can specify multiple in array ["Tests.Logs"]Forearm
It says: Stage must be a properly formatted document. When I do thatSalutation
yes this is not formatted { "$Tests.Logs":1 } as per $unset syntax just try only "Tests.Logs". remove objectForearm
I also tried your suggestion { "Tests.Logs" }. which says "Stage must be a properly formatted document."Salutation
please read the documentation provided in first comment, just use only string "Tests.Logs", remove everything and don't wrap in object => remove this { }.Forearm
Oh ok. It Works now. ThanksSalutation

© 2022 - 2024 — McMap. All rights reserved.