Mongo Basics, Chapter 3, IDE - error received using updateMany

I received the following error when trying to add fields to documents in the collection. Not sure what the error is, or how to fix it?

MongoDB Enterprise atlas-4actmy-shard-0:PRIMARY> db.zips.updateMany({“state”:“NY”},{“city”:“ALBANY”},{"$set":{“capital?”:“true”}})
2020-11-01T19:58:56.315+0000 E QUERY [js] Error: the update operation document must contain atomic operators :
DBCollection.prototype.updateMany@src/mongo/shell/crud_api.js:625:1
@(shell):1:1

Welcome @Christine_Owens! :slight_smile:

Just a suggestion:

  • Do not modify the original dataset
    Then may get wrong answers to future Labs.

Now, you may read the docs for update many, but:

  • The first argument is a single object filter {state:"NY", city:"ALBANY"}
  • prototype is db.collection.updateMany(<filter>,<update>,<options>)

thanks, I figured this out while waiting for an answer. As for modifying the original dataset, I am following the directions in the end of the chapter3 IDE quiz questions.
I appreciate your help

No worries @Christine_Owens.

I kept wondering what exactly the error was. It seems to be mongo looking for {$set...} in the <update> slot. (but instead “city” is there).

Just in case you were wondering too.

Good luck for the rest.

Hi @Christine_Owens,

As @Santiago_Miranda mentioned your second expression {“city”:“ALBANY”} should be enclosed in the first set of curly braces.

The updateMany() method is interpreting it as the update expression.

You can see the syntax here : https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/index.html

~ Shubham

I am wondering if the bolean value ‘true’ should be in quotation marks or not in using the updateMany command. As in:

b.zips.updateMany({ “city”: “ALBANY”, “state”: “NY”}, {"$push": {“capital”: true})

Could anyone elaborate on that since I did not get any feedback from the in-browser IDE space ?

Hi @cisko

true is a data type, same as a int or str. If you quote it, it’s converted to string.

Then, it depends on what you plan to do with that field. A boolean value (as you did) looks fine.

By the way, the expression is missing a } after “true”.

No quite exact. true is a value, the type is bool or Boolean depending of the context. And string the type is string or String, not str.

Fun to have in the shell:

> db.test.drop()
true
> db.test.insertOne( { a : true } )
{
	"acknowledged" : true,
	"insertedId" : ObjectId("5fb7ca76379f44ea6762c856")
}
> db.test.find( { a : true } )
{ "_id" : ObjectId("5fb7ca76379f44ea6762c856"), "a" : true }
> db.test.find( { a : "true" } )
> db.test.aggregate( { $addFields : { "type_of_a" : { "$type" : "$a" } } } )
{ "_id" : ObjectId("5fb7ca76379f44ea6762c856"), "a" : true, "type_of_a" : "bool" }

Alright, I’ll check that out, but I’m not trying to write perfect sentences. Just to give some hints or a point of view; that hopefully will be correct.

“true is a data type”, obviously refers to boolean, and with “int” and “str”, I mean that you can test this:

"$type":"str" or "$type":"int", I didn’t check it in the manual, so maybe it’s not precise, but yet I guessed someone would understand.

By writing a:true if it’s not a boolean, it’s rather strange. You’re typing a superset of JSON, and in JSON that’s a boolean.

Also, in the particular example the person is citing, it’s just the definition of update and the type is <boolean> I can hardly imagine how what I said is confusing.

Thank you all for the help.

Forgot the } indeed :frowning:

But your answers helped a lot.

Thanks again :slight_smile:

1 Like