mongodbstudio3t

Running an Update Many MongoDb Query v6 failing in Studio 3t IntelliShell


I am trying to run an update many query in Studio 3t's intellishell using MongoDB v.6.0.9.

This is my query:

db.address_db.updateMany({
  "geometry.coordinates": {
    "$geoWithin": {
      "$box": [
        [-134.4, 58.3],
        [-134.3, 58.4]
      ]
    }
  }, {
      $set: {"properties.region": "AK"}
  }
});

If I run just a find({}) with the same filter criteria, it returns the proper results. But when running the updateMany() I get 2 errors. One is a pop up from Studi 3T that says:

A syntax error was detected at the runtime. Please consider using a higher shell version or use the syntax supported by your current shell.

The 2nd error in the shell output is:

Uncaught:

SyntaxError: Unexpected token (9:5)

   7 | 58.4]]
   8 |     }
>  9 |   }, {
     |      ^
  10 |

Uncaught:

SyntaxError: Missing semicolon. (2:25)

  1 |     $set:{
> 2 |       "properties.region":"AK"}}});
    |                          ^
  3 |

I am unusure if my syntax is wrong or if I need to update my syntax becuase it's too old? Too new? I try adding a semi colon where it tells me but the intellishell freaks out even more with errors. Thanks!


Solution

  • As the message suggests, you have a syntax error. The updateMany() call is going to take two arguments, you're presently passing it a single one. This is because your trailing curly bracket needs to be moved forward to close the first argument (the query object) before the update object is specified.

    The corrected version is:

    db.address_db.updateMany({
      "geometry.coordinates": {
        "$geoWithin": {
          "$box": [
            [
              -134.4,
              58.3
            ],
            [
              -134.3,
              58.4
            ]
          ]
        }
      }
    },
    {
      $set: {
        "properties.region": "AK"
      }
    });
    

    You can see the failing one here and the working one here in the playground.