Type 'ObjectId' is not assignable to type 'never'

Why do i get this typescript error when i create a typeless collection when i try to use $pull on the testCollection? When i use non-array functions like $find etc. it works fine. Do i have to type every Collection? How would it work with dynamic Collections-Documents?

    public async foo()
    {
        let testCollection = Database.db.collection("test");

        await testCollection.updateMany({ name:"foo" }, {
            $pull: { ids: "bar" }
        }).catch((error)=>{})
    }
Type 'string' is not assignable to type 'never'.

I use mongodb@4.2.2 and typescript@3.7

1 Like

Can confirm, I’m having the same issue.
It turns your you need to pass a generic type to the collection, like so:

collection<{}>("test").updateMany({ name:"foo" }, {
            $pull: { ids: "bar" }
        })

It just looks silly to me, because to my understanding, if I’m not passing a schema to the collection, it should just allow anything. Also because it works with $set.

3 Likes

Thanks for the reply, it helped solve my problem.