If I would like to push {name: “c”, price: 30, type: “d”} into the market which name = "apple”, what should I do in mongoose? but if there is already has name="c” and type = "d” inside the market array, I will update the existing data instead of pushing new data, I am using typescript and mongoose.
Hi @WONG_TUNG_TUNG, so there are two approaches to this, the way simpler version (works for almost all use-cases) and then the performance-aware version (overly complex, only use if you are trying to be as performant as possible)
Simpler version - just mutate the data as you would an object and then save in typescript, and since you are using mongoose, it’s way easier -
in this solution, I need perform two insertion, one is the not equal(not existing) case to push the data, another is the equal(existing) case to update the data right? I thought there will be one command/function to achieve it instead of two