NMW03
NMW03

Reputation: 117

mongoose: Can't remove item from an array

This is my code:

function remove(guild, u) {
 return db.updateOne({ guild }, {
  $pull: { "feeds": { "user": { "screen_name": u } } }
 })
}

When I run remove("718471604291764295", "twitter") it doesn't remove. My schema is:

{
    guild: String,
    feeds: [{
        channel: String,
        webhook: {
            id: String,
            token: String,
        },
        user: {
            id: String,
            screen_name: String,
        }
    }]
}

What's wrong with this?

Upvotes: 1

Views: 44

Answers (1)

turivishal
turivishal

Reputation: 36114

  • use async/await in your function
  • just write user.screen_name: u
async function remove(guild, u) {
    return await db.updateOne(
        { guild: },
        {
            $pull: {
                feeds: {
                    "user.screen_name": u
                }
            }
        }
    )
}

Playground

Upvotes: 1

Related Questions