[DOCS-10557] error when use a $switch Created: 20/Jul/17  Updated: 27/Oct/23  Resolved: 21/Jul/17

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Docs Collector User (Inactive) Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: collector-298ba4e7
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

mongodb

Location: https://docs.mongodb.com/manual/reference/operator/aggregation/switch/
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/64.4.144 Chrome/58.4.3029.144 Safari/537.36
Referrer: https://www.google.com.vn/
Screen Resolution: 1366 x 768


Participants:
Days since reply: 6 years, 29 weeks, 6 days ago

 Description   

I use a query:
db.cashbooks.aggregate( [
{
$project:
{
"name" : "$deal_code",
"summary" :
{
$switch:
{
"$created_userId": [
{
case:

{ $in : [ 1,2,3 ] }

,
then: 1
}
],
default: 2
}
}
}
}

and I get the Error:
Assert: command failed:

{ "ok" : 0, "errmsg" : "$switch found an unknown argument: $created_userId", "code" : 40067, "codeName" : "Location40067" }

: aggregate failed



 Comments   
Comment by Ravind Kumar (Inactive) [ 20/Jul/17 ]

Hello,

The syntax for $switch is as follows:

 
$switch: {
   branches: [
      { case: <expression>, then: <expression> },
      { case: <expression>, then: <expression> },
      ...
   ],
   default: <expression>
}

You need to adjust your example code to adhere to the syntax for $switch by wrapping your case statements in a `branch` array.

db.cashbooks.aggregate( 
   [ 
      { $project: 
         { 
            "name" : "$deal_code", 
            "summary" : { 
               $switch: {
                  branches: [
                     { case : { $in : [ "$created_userId" , [1 , 2, 3] ] }, then :  <thing> }
                  ],
                  default : 2 
               } 
            } 
         } 
      }
   ]
)

See https://docs.mongodb.com/manual/reference/operator/aggregation/switch/ for more information

Generated at Thu Feb 08 08:00:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.