MongoError: '$set' is empty. You must specify a field like so

  const update = {
     $set: { date: },

  col.updateMany({}, update)

Where col is the connection to my database. However, when I run this code I get an error that ‘$set’ is empty, even though it’s the correct type (object) and is truthy. What am I missing?

I added an error catcher. This is the output:

MongoError: '$set' is empty. You must specify a field like so: {$set: {<field>: ...}}
    at Function.create (/home/runner/gli-forktery-1/node_modules/mongodb/lib/core/error.js:57:12)
    at toError (/home/runner/gli-forktery-1/node_modules/mongodb/lib/utils.js:123:22)
    at /home/runner/gli-forktery-1/node_modules/mongodb/lib/operations/common_functions.js:379:39
    at handler (/home/runner/gli-forktery-1/node_modules/mongodb/lib/core/sdam/topology.js:942:24)
    at /home/runner/gli-forktery-1/node_modules/mongodb/lib/cmap/connection_pool.js:350:13
    at handleOperationResult (/home/runner/gli-forktery-1/node_modules/mongodb/lib/core/sdam/server.js:558:5)
    at MessageStream.messageHandler (/home/runner/gli-forktery-1/node_modules/mongodb/lib/cmap/connection.js:277:5)
    at MessageStream.emit (events.js:315:20)
    at processIncomingData (/home/runner/gli-forktery-1/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/home/runner/gli-forktery-1/node_modules/mongodb/lib/cmap/message_stream.js:42:5) {
  driver: true,
  index: 0,
  code: 9

Interestingly enough the error seems to originate from MongoDB itself, not my code:

So, turns out part of the error was brackets:

{update}, //curly braces here 
	upsert: false,
	multi: true
}).catch(e => console.log(e))

But now, it throws a new error:

      throw new TypeError('Update document requires atomic operators');

TypeError: Update document requires atomic operators

At least this one directly references my code… lines 11-13 are:

  const update = {
     $set: { date:, },

What’s an atomic?

Did you mean to use with parentheses? I think maybe mongo would have stripped out the field date: because the value was a function.

1 Like

Hmmm… maybe I’ll move outside and make it a variable. Thanks for the suggestion!

var now = //type number
  const update = {
    date: now, 

  col.updateMany({}, {update}, {upsert:false, multi: true})
  .then(_ => console.log("success"))
  .catch(e => console.log(e))

Still returns an error…

I’m skeptical of the “brackets” change you made earlier. I would have tried

  const update = {
     $set: { date: },

  col.updateMany({}, update)
1 Like

I actually tried that exact code (col.updateMany({}, update)), but it threw that “$set is empty” error.
I should probably backtrack and do some testing with update just to get familiar with it again.

I ran a quick test, and this works:

const updateDoc = {
	$set: {
	"_id": ObjectId("5ef4405c150cac012dad6146")
}, updateDoc)
  .then(_ => console.log("success"))
  .catch(e => console.log(e)) was passed successfully into the database. I suspect update is a reserved word in Javascript? In any case, I’ll work this into my dev repo right now.

It worked!