1

I have a calendar document like below

calendar: [
 {days: {
  1: [],
  2: ['surprise'],
  3: [],
  ...
 }},
 {days: {
  1: [],
  2: [],
  3: ['test'],
  ...
 }}
]

I'm trying to find words wherever are they then pull them from array. Here's my code:

var words = ['test']
Calendar.update(
    {$or: [
      {"calendar.days.1": {$in: words}},
      {"calendar.days.2": {$in: words}}
    ]},
    {$pull:
      {$or: 
       {"calendar.days.1": {$in: words}},
       {"calendar.days.2": {$in: words}}
      }
    },
    {multi: true}
  )

First part is working correctly. It finds documents which have words. But in the second part it's not deleting the words from array. Returns that log: { ok: 0, n: 0, nModified: 0 }

Any helps?

2
  • 1
    Is days an array or an object with keys 1, 2, 3? Commented Jul 29, 2021 at 9:57
  • Sorry. Days is not array. It is object Commented Jul 29, 2021 at 10:02

1 Answer 1

1

You can use $[] positional operator after calendar field in update part,

var words = ['test'];
await Calendar.update(
  {
    $or: [
      { "calendar.days.1": { $in: words } },
      { "calendar.days.2": { $in: words } }
    ]
  },
  {
    $pull: {
      "calendar.$[].days.1": { $in: words },
      "calendar.$[].days.2": { $in: words }
    }
  },
  { multi: true }
)

Playground

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.