So when fiddling with data in mongo using mongoose I am having a difficult time with setting data to a document
This issue is when I use $set in findByIdAndUpdate it sets the entire document to when I put it in, which is bad.
So then I did some research and I tried using $push from a previous project but it turns out that $push doesn't work in the array (Makes sense)
So then how would I both $set data to a document that only updates the fields to which I am updating data?
This may seem like a simple question but I am not the best problem solver, with mongooses documents not being the best I struggle to find a lot of answers to my problems in the documents
This is the code I have in my Code Base
await userSchema.findByIdAndUpdate(attackerUser._id, { $push : { duelInfo: { dueling: false, duelingWith: "", timeSinceOffer: 0} }, $inc : { duelinfo: { wins : 1 }, credits : user.duelInfo.duelBet } }, { upsert : true } );
await userSchema.findByIdAndUpdate(attackerUser._id, { $inc : { duelinfo: { wins : 1 }, credits : user.duelInfo.duelBet } }, { upsert : true } );
await userSchema.findByIdAndUpdate(defenderUser._id, { $push : { duelInfo: { dueling: false, duelingWith: "", timeSinceOffer: 0} } }, { upsert : true } );
await userSchema.findByIdAndUpdate(defenderUser._id, { $inc : { duelinfo: { losses : 1 } }, credits : - user.duelInfo.duelBet }, { upsert : true } );
This is what the Documents look like
_id: "195967294"
username:"emansza"
collectInfo: true
credits: 5000
processSign: false
messageCount: 0
duelInfo:Object
dueling: false
duelingWith: ""
lastDuel: 2022-01-06T02:09:46.340+00:00
wins: 1
losses: 1
draws: 1
duelBet: 1