M001: MongoDB Basics Issue on [Your Chapter 3 IDE space]

Hello,
I’m working on “[Your Chapter 3 IDE space]”
"In the sample_training.zips collection add a boolean field “capital?” to all
documents pertaining to Albany NY, and New York, NY. The value of the field
should be true for all Albany documents and false for all New York documents."

I know it is easy to update by using 2 update requests to achieve the task :smile:

db.zips.updateMany({"state": "NY", "city":"ALBANY"},{"$set":{"capital?":true}})
following by this below:
db.zips.updateMany({"state": "NY", "city":"NEW YORK"},{"$set":{"capital?":false}})

but I think it’s possible to do it by using a single request and I tried this one:

and I started to find all documents matching the exercise by using this request:
db.zips.find({“state”: “NY”, “city”:{"$in":[“ALBANY”,“NEW YORK”]}})
now I tried to update:

db.zips.updateMany({state: "NY", city:{$in:["ALBANY","NEW YORK"]}},
                                 {$set:
                                      { capital?:
                                            {$cond: 
                                               [ { $eq: [ "$city", "ALBANY" ] }, true, false ]
                                             }
                                      } 
                                  })

but I have this error:

2021-06-08T14:19:39.140+0000 E QUERY [js] SyntaxError: missing : after property id @(shell):3:47

You query is not formatted correctly.

Most likely the key capital? causes a parsing error. To prevent things like that I prefer to put all keys in double quotes, this includes "state", “city”, “$set”, “$in”, “capital?”, “$cond” and "$eq".

2 Likes

ok !! Let me try to well format the code. Thank you steevej

I tried there is another error:

WriteError@src/mongo/shell/bulk_api.js:461:48
Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:841:49
Bulk/executeBatch@src/mongo/shell/bulk_api.js:906:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21
DBCollection.prototype.updateMany@src/mongo/shell/crud_api.js:655:17
@(shell):1:1

The request :

    db.zips.updateMany({"state": "NY", "city":{"$in":["ALBANY","NEW YORK"]}},
                                     {"$set":
                                          { "capital?":
                                                {"$cond": 
                                                   [ { "$eq": [ "$city", "ALBANY" ] }, true, false ]
                                                 }
                                          } 
                                      })

All your quotes are wrong except the one around $in, $set and $cond.

Most likely the query has been cut-and-pasted from badly formatted html.

See Formatting code and log snippets in posts to find how to properly format code and JSON documents.

2 Likes