Deleting document inside other document

This is my user a user has an array of card objects

const userSchema=new mongoose.Schema({

    username:{

        type:String,

        required:true

    },

    email:{

        type:String,

        required:true

    },

    password:{

        type:String,

        required:true

    },

    status:{

        type:Boolean,

        default:false

    },

    cards:{

        type:[cardSchema],

        default:[]

    }

})

This is my card

const cardSchema=new mongoose.Schema({
   name:{
    type:String,
    required:true,
   },
   startTime:{
    type:Date,
    required:true
   },
   endTime:{
    type:Date,
    required:true
   },
   description:{
    type:String,
   }
})

When i delete a particular card from card collection its not deleted from that user’s array how to solve this i want that the a particular card be flushed out of whole db including the user’s array which contains that card

1 Like

Hello @Assassin_N_A, Welcome to the MongoDB community forum,

Could you please share what query you tried and not deleting the elements?

1 Like

I haven’t tried any query i used went to atlas to delete a particular card from card table just to check if the same card is also deleted from array within user’s card

As I can see in your schema, there is only one parent users schema, and cards is an array that is sub schema, so there should be one collection users, how cards is a different collection?

Your question is still not clear to me, if you want to delete elements from cards then you can use update methods (updateOne, updateMany) query with $pull operator and positional operators ($ positional, and $[<identifier>] positional filtered).

1 Like

Apart from the previous advice which is correct you can test queries in https://mongoplayground.net

You can also share a link to the playground for anything you need help with.

Pd: remember that Cards is not a collection, it is just a shape for the elements in the array. I wonder if you are trying to store a reference to a different collection instead.

Well when i ran my code i’ve sent above two tables were created(sorry i dont know terminologies that well i’m more familiar with SQL terms) and this is what i have tried in atlas i expect the card inside user’s array to be deleted as well but it is still there
here’s what i tried in atlas @santimir