chichi
chichi

Reputation: 3292

Mongoose: deleting null value in the array?

I have an array like below for multiple documents.

meta: [1, null]

I want to get rid of the null value. So the array just looks like

meta: [1]

I tried with $unset

.updateMany({}, { $unset: { 'meta.1': 1 } }, { multi: true });

However, it doesn't remove null value. How can I keep the value 1 but just take off null from the array?

Upvotes: 1

Views: 430

Answers (1)

turivishal
turivishal

Reputation: 36104

Try $pull to remove element from array, and updateMany() not require multi: true option,

.updateMany(
  { meta: null }, 
  { $pull: { meta: null } }
)

Upvotes: 1

Related Questions