0

client.schema is the name of my collection

When i run this code:

client.schema.update(
  {},
  { $set: {"newField": 1} },
  false,
  true
)

It doesn't create a new field in every document called newField, but instead returns this error:

MongooseError: Callback must be a function, got true

How would I add the field to all documents?

Bob joe12
  • 103
  • 22
  • 1
    have you checked the function definition? Your 4th parameter is expected to be a function, not a boolean value – Aziza Kasenova Aug 31 '21 at 23:00
  • Someone told me to say true for this reason: Multi - update multiple documents if matches to query hence true – Bob joe12 Aug 31 '21 at 23:01
  • I think you are taking about https://mongoosejs.com/docs/api/model.html#model_Model.update – Aziza Kasenova Aug 31 '21 at 23:05
  • Here is where I was told to do this https://stackoverflow.com/questions/68991659/how-to-add-field-to-all-documents-in-database-mongoose?noredirect=1#comment121933222_68991659 – Bob joe12 Aug 31 '21 at 23:06

1 Answers1

0
client.schema.update(
  {},
  { $set: {"newField": 1} },
  {upsert:false,multi:true} 
)

You should add newField to your schema

mohammad Naimi
  • 2,259
  • 1
  • 5
  • 15