Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
Trying to push data from a object n mongo

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
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!