Need correct MQL for chapter 3 IDE based question , although my answer is appoved through test run

Please provide correct MQL for below question

People often confuse NEW YORK City as the capital of New York state, when in
reality the capital of New York state is ALBANY.

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 document s and false for all NEW YORK documents.

What have you tried so far? What were the results?

db.zips.updateMany( [{ "state": "AL" }, { "$set": { "capital?": "true"} } , { "state": "NY" }, { "$set": { "capital?": "false"} }])

Some basic fault finding:

  • Have you navigated to the correct collection before the above query?

use sample_training

The problem statement talks about state of New York and the cities New York and Albany.

The query

is related to the state of Alabama.

Reading the question provided above, (I haven’t checked back to Chapter 3), your being asked to ADD a NEW field, not UPDATE an EXISTING.

Your query begins with updateMany, it looks like it should begin with insert or insertMany

Please read the documentation and look at the example at https://docs.mongodb.com/manual/reference/operator/update/set/.

Doing a $set on a non-existing field adds the field to an existing document.

The function insert and insertMany will insert whole documents, they will not add a field to an existing document.

// Collection before $set
> db.c.find()
{ _id: 1, a: 0 }

> db.c.update( { _id:1 } , { "$set" : { b:2 }})

// Collection after $set, field b added to existing document
> db.c.find()
{ _id: 1, a: 0, b: 2 }
1 Like