0

So I have a MongoDB collection where there are three variables for every document:

const moneySchema = mongoose.Schema({
  userID: String,
  tron: Number,
  sccn: Number
})

Every hour with JavaScript I want to increase the sccn value by (tron * multiplier) with multiplier being a previously defined variable.

I am aware on how to schedule the hourly event but how would I do the increment? I have done this so far.

Money.updateMany(
    { },
    { $inc: { "sccn": { $mul: { "tron": sccnpertrx } } } } }
).catch(err => console.log(err));
Shay
  • 57
  • 1
  • 4

1 Answers1

0

This (mongo shell) query will update the money collection with the increment:

Input documents:

{ _id: 1, tron: 5, sccn: 6 },
{ _id: 2, tron: 11, sccn: 3 }

Update query:

var sccnpertrx = 2;

db.money.find().forEach( doc => { let incVal = doc.tron * sccnpertrx; db.money.updateOne( {_id: doc._id }, { $inc: { sccn: incVal } } ) } );

Updated documents:

{ "_id" : 1, "tron" : 5, "sccn" : 16 }
{ "_id" : 2, "tron" : 11, "sccn" : 25 }
prasad_
  • 12,755
  • 2
  • 24
  • 36